Home > Backend Development > C#.Net Tutorial > C# xml deserialization code example details

C# xml deserialization code example details

黄舟
Release: 2017-03-03 11:47:28
Original
1284 people have browsed it

XML deserialization is very convenient, example:

 [XmlRoot(Root = "result")]
    public class UniMsgSetResult
    {
        [XmlAttribute("resultCode")]
        public int resultCode;


        [XmlElement("uniMsgSet")]
        public UniMsgSet uniMsgSet;

    }
Copy after login
<result resultCode="0">
<UniMsgSet>...</UniMsgSet>
</result>
Copy after login

Collection type example:

[XmlRoot("result")]
    public class GetDiskInnerResult
    {
        public string parentCatalogID; //String32 待查询目录的父目录ID。如果当前目录为root,则父目录ID为空。


        


        [XmlArray("catalogList"), XmlArrayItem("catalogInfo")]
        public List<CatalogInfo> catalogList;// CatalogInfo[] 查询节点下的目录列表


        [XmlArray("contentList"), XmlArrayItem("contentInfo")]
        public List<ContentInfo> contentList; // ContentInfo[] 查询节点下的内容列表
    }
Copy after login
 <result>
    <parentCatalogID>1</parentCatalogID>
    <catalogList>
    <catalogInfo>...</catalogInfo>
    <catalogInfo>...</catalogInfo>
    </catalogList>


    <contentList>
    <contentInfo>...</contentInfo>
    <contentInfo>...</contentInfo>
    </contentList>
    </result>
Copy after login

If you want to add attributes to the collection entity:

In order words, add an attribute to an object element after xml serialization,
If you want something like,

<Rats count=“2″>
  <Rat>little rat</Rat>
  <Rat>old rat</Rat>
</Rats>
Copy after login

The C# code is

[XmlType(“Rats”)]
    public class Rats
    {
        [XmlAttribute(“count”)]
        public int Count { get; set; }
        [XmlElement(“Rat”)] // now the array element will be as same as the object element Rats. 
        public string[] Rat { get; set; }
    }
Copy after login


Traditional xml array serialization would get the extra element for the array itself.

[XmlType(“Rats”)]
    public class Rats
    {
        [XmlAttribute(“count”)]
        public int Count { get; set; }
        [XmlArray(“Rats”)]
        [XmlArrayItem(“Rat”)]
        public string[] Rat { get; set; }
    }
Copy after login
<Rats count=“2″>
  <Rats>
    <Rat>little rat</Rat>
    <Rat>old rat</Rat>
  </Rats>
</Rats>
Copy after login

The above is the detailed introduction of the C# xml deserialization code example, and more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template