简体   繁体   English

序列化XML数组,属性为Object

[英]Serialize XML array, attribute to Object

How Can I define an object to deserialize the following XML: 如何定义对象以反序列化以下XML:

<body>
<S1 A="1">
    <S2 B="1">
        <S3 C="1"/>
        <S3 C="1"/>
    </S2>
    <S2 B="2"/>
</S1>
<S1 A="2"/>

I'd strongly recommend to use xsd.exe , which can help in generating XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly. 我强烈建议使用xsd.exe ,它可以帮助从XDR,XML和XSD文件或从运行时程序集中的类生成XML模式或公共语言运行时类。

  1. Open VS Developer Command Prompt 打开VS Developer命令提示符
  2. Type xsd.exe PathToXmlFile.xml /outputdir:OutputDir and press Enter - this will generate *.xsd file 键入xsd.exe PathToXmlFile.xml /outputdir:OutputDir并按Enter -这将生成*.xsd文件
  3. Type xsd.exe PreviouslyCreatedXsdFile.xsd /classes /outputdir:OutputDir and press Enter - this will generate *.cs file (class definition). 键入xsd.exe PreviouslyCreatedXsdFile.xsd /classes /outputdir:OutputDir并按Enter -这将生成*.cs文件(类定义)。

That's all! 就这样!

Try! 尝试!

Try this.... 尝试这个....

Usings..... 正在使用.....

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

Classes..... 课.....

[XmlRoot(ElementName = "S3")]
public class S3
{
    [XmlAttribute(AttributeName = "C")]
    public string C { get; set; }
}

[XmlRoot(ElementName = "S2")]
public class S2
{
    [XmlElement(ElementName = "S3")]
    public List<S3> S3 { get; set; }
    [XmlAttribute(AttributeName = "B")]
    public string B { get; set; }
}

[XmlRoot(ElementName = "S1")]
public class S1
{
    [XmlElement(ElementName = "S2")]
    public List<S2> S2 { get; set; }
    [XmlAttribute(AttributeName = "A")]
    public string A { get; set; }
}

[XmlRoot(ElementName = "body")]
public class Body
{
    [XmlElement(ElementName = "S1")]
    public List<S1> S1 { get; set; }
}

Code..... 码.....

        string strXML = File.ReadAllText("xml.xml");
        byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
        MemoryStream ms1 = new MemoryStream(bufXML);

        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(Body));
        try
        {
            using (XmlReader reader = new XmlTextReader(ms1))
            {
                Body deserializedXML = (Body)serializer.Deserialize(reader);

            }// put a break point here and mouse-over deserializedXML….
        }
        catch (Exception ex)
        {
            throw;
        }

Your XML..... 您的XML .....

<body>
<S1 A="1">
    <S2 B="1">
        <S3 C="1"/>
        <S3 C="1"/>
    </S2>
    <S2 B="2"/>
</S1>
<S1 A="2"/>
</body>

I added the end tag..... I am reading your XML in to a string from a file in the application build folder called xml.xml... you will need to get the XML string from somewhere else or create the xml.xml file and save your XML for the code above to work 我添加了结束标签.....我正在从应用程序构建文件夹xml.xml中的文件中读取XML字符串,您将需要从其他位置获取XML字符串或创建xml。 xml文件,并保存XML以使上面的代码正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM