.NETCore C # Intermediate Part 2-6 Json and XML

.NETCoreCSharp Intermediate articles 2-6

This section is Json and XML operations

Brief introduction

Json and XML text is a common computer network communication in text format, which is actually Json array in JavaScript object, reflects an object-oriented way, and it is an XML markup language, similar to our html tags he is more of a manifestation of hierarchy.
But no matter what kind of text format, we have to learn necessary.

JSON

First of all, tell us about Json:
Json is actually inside of JavaScript objects and arrays, through different combinations, can constitute many different data structures. Which is the object using curly braces, brackets is an array, for example:

{
"data":
    {
        "people":
        [
            {"name":"ppppp","age":18}
        ]
    },
"status":0
}

There, data is an object, and the people is an array.

If you want to deal with Json data, you can find many on nuget appropriate library, where I use the LitJson, this may be a rare library, but I feel very good use.

Here I give our free api address https://www.sojson.com/api/weather.html , where you can request to our json text.

For LitJson, it fully illustrates the core of our Json - and an array of objects. For LitJson, array using List Objects directly create a class for processing. For the above example, we can construct the following class relationships

public class data
{
    public List<People> people{ get; set; }
    public int status{ get; set; }
}
// 下面操作将json文本转换成data的对象
Main()
{
    JsonContent jsonc = JsonMapper.ToObject<data>(json);
    foreach(var t in data)
}

I can see more of this blog Json handle combat , and LitJson official website

XML

XML is widely used in a common text format information exchange network, his writing is somewhat similar to our html, as said before, he is more explained a hierarchical relationship. For example, the following format is a common xml text.

<Computers>
  <Computer ID="11111111" Description="Made in USA">
    <name>Surface</name>
    <price>5000</price>
  </Computer>
  <Computer ID="2222222" Description="Made in USA">
    <name>IBM</name>
    <price>10000</price>
  </Computer>
    <Computer ID="3333333" Description="Made in USA">
    <name>Apple Mac</name>
    <price>10000</price>
  </Computer>
</Computers>

In C #, XML same operation we have many libraries, where we use the XmlDocument to operate.

XmlDocument class common methods:

  • Load (string path) to load the file path Xml
  • SelectSingleNode (string node) selected nodes
  • The ChildNodes, property is not a function, for obtaining all the child nodes, the object returns XmlNodeList
  • HasChildNodes attribute, determines whether a child node
  • CreateElement create a new node
  • AppendChild (XmlElement node) is added subnode
  • InsertBefore (XmlElement node, XmlElement ChildeNodes) forwardly inserted
  • SetAttribute (string name, string value) for the specified node and assignment of new properties
  • InnerText property, access to the internal text
  • Save () to save
    XmlDocument myXmlDoc = new XmlDocument();
    //加载xml文件(参数为xml文件的路径)
    myXmlDoc.Load(xmlFilePath);
    //获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
    XmlNode rootNode = myXmlDoc.SelectSingleNode("Computers");
    //分别获得该节点的InnerXml和OuterXml信息
    string innerXmlInfo = rootNode.InnerXml.ToString();
    string outerXmlInfo = rootNode.OuterXml.ToString();
    //获得该节点的子节点(即:该节点的第一层子节点)
    XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
    foreach (XmlNode node in firstLevelNodeList)
    {
        //获得该节点的属性集合
        XmlAttributeCollection attributeCol = node.Attributes;
        foreach (XmlAttribute attri in attributeCol)
        {
            //获取属性名称与属性值
            string name = attri.Name;
            string value = attri.Value;
            Console.WriteLine("{0} = {1}", name, value);
        }

        //判断此节点是否还有子节点
        if (node.HasChildNodes)
        {
            //获取该节点的第一个子节点
            XmlNode secondLevelNode1 = node.FirstChild;
            //获取该节点的名字
            string name = secondLevelNode1.Name;
            //获取该节点的值(即:InnerText)
            string innerText = secondLevelNode1.InnerText;
            Console.WriteLine("{0} = {1}", name, innerText);
            //获取该节点的第二个子节点(用数组下标获取)
            XmlNode secondLevelNode2 = node.ChildNodes[1];
            name = secondLevelNode2.Name;
            innerText = secondLevelNode2.InnerText;
            Console.WriteLine("{0} = {1}", name, innerText);
        }
    }
}
    catch (Exception ex)
    {
         Console.WriteLine(ex.ToString());
    }

Bloggers are not commonly used xml, more information, please refer to the Microsoft official documentation and https://www.cnblogs.com/zhengwei-cq/p/7242979.html this blog

If my post helped you, you github.NETCoreGuide project helped me a star, a concern and recommendation in the garden midpoint blog.

Github

BiliBili Home

WarrenRyan'sBlog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11415716.html