none
【json.net】xml转换为json格式时,如何将指定节点转换成数组 RRS feed

  • 问题

  • <specialty nameCN="节点A">
            <step>
                <signer staffID="800706" nameCN=""><![CDATA[]]></signer>
            </step>
            <step>
                <signer staffID="090477" nameCN=""><![CDATA[]]></signer>
            </step>
        </specialty>
        <specialty nameCN="节能B">
            <step>
                <signer staffID="800608" nameCN=""><![CDATA[]]></signer>
                <signer staffID="800808" nameCN=""><![CDATA[]]></signer>
            </step>
            <step>
                <signer staffID="800602" nameCN=""><![CDATA[]]></signer>
                <signer staffID="800803" nameCN=""><![CDATA[]]></signer>
            </step>
        </specialty> step下有多个signer节点时,输出结果signer是数组,

    只有1个signer节点 输出signer不是数组,如何在只有一个signer节点时也输出为数组? 谢谢


    2018年6月30日 16:11

答案

  • 你好,

    你可以先把你的XML转换成C#的类,在把类对象序列化成Json. 下面的类你可以参考一下:

    [XmlRoot(ElementName = "signer")]
        public class Signer
        {
            [XmlAttribute(AttributeName = "staffID")]
            public string StaffID { get; set; }
            [XmlAttribute(AttributeName = "nameCN")]
            public string NameCN { get; set; }
        }
    
        [XmlRoot(ElementName = "step")]
        public class Step
        {
            [XmlElement(ElementName = "signer")]
            public List<Signer> Signer { get; set; }
        }
    
        [XmlRoot(ElementName = "specialty")]
        public class Specialty
        {
            [XmlElement(ElementName = "step")]
            public List<Step> Step { get; set; }
            [XmlAttribute(AttributeName = "nameCN")]
            public string NameCN { get; set; }
        }
    
        [XmlRoot(ElementName = "root")]
        public class Root
        {
            [XmlElement(ElementName = "specialty")]
            public List<Specialty> Specialty { get; set; }
        }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lovenet2010 2018年10月24日 13:34
    2018年7月2日 3:44
    版主

全部回复

  • 补充下,意思就是说,xml转成json时,
    step下有多个signer节点时,输出结果signer是json数组形式,

    只有1个signer节点时 输出signer不是json数组形式,

    如何在step只有一个signer节点时也输出为json数组?

    2018年7月1日 2:59
  • 你好,

    你可以先把你的XML转换成C#的类,在把类对象序列化成Json. 下面的类你可以参考一下:

    [XmlRoot(ElementName = "signer")]
        public class Signer
        {
            [XmlAttribute(AttributeName = "staffID")]
            public string StaffID { get; set; }
            [XmlAttribute(AttributeName = "nameCN")]
            public string NameCN { get; set; }
        }
    
        [XmlRoot(ElementName = "step")]
        public class Step
        {
            [XmlElement(ElementName = "signer")]
            public List<Signer> Signer { get; set; }
        }
    
        [XmlRoot(ElementName = "specialty")]
        public class Specialty
        {
            [XmlElement(ElementName = "step")]
            public List<Step> Step { get; set; }
            [XmlAttribute(AttributeName = "nameCN")]
            public string NameCN { get; set; }
        }
    
        [XmlRoot(ElementName = "root")]
        public class Root
        {
            [XmlElement(ElementName = "specialty")]
            public List<Specialty> Specialty { get; set; }
        }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lovenet2010 2018年10月24日 13:34
    2018年7月2日 3:44
    版主