序列化XML数组,属性为Object
Serialize XML array, attribute to Object
我如何定义一个对象来反序列化以下 XML:
<body>
<S1 A="1">
<S2 B="1">
<S3 C="1"/>
<S3 C="1"/>
</S2>
<S2 B="2"/>
</S1>
<S1 A="2"/>
我强烈建议使用 xsd.exe,这有助于从 XDR、XML 和 XML 中生成 XML 模式或公共语言运行时 classes =35=] 文件,或来自运行时程序集中的 classes。
- 打开VS Developer Command Prompt
- 键入
xsd.exe PathToXmlFile.xml /outputdir:OutputDir
并按 Enter
- 这将生成 *.xsd
文件
- 键入
xsd.exe PreviouslyCreatedXsdFile.xsd /classes /outputdir:OutputDir
并按 Enter
- 这将生成 *.cs
文件(class 定义)。
就这些了!
试试吧!
试试这个....
使用.....
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
类.......
[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; }
}
代码......
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;
}
你的XML.......
<body>
<S1 A="1">
<S2 B="1">
<S3 C="1"/>
<S3 C="1"/>
</S2>
<S2 B="2"/>
</S1>
<S1 A="2"/>
</body>
我添加了结束标记.....我正在将您的 XML 读入应用程序构建文件夹中名为 xml.xml 的文件中的字符串...您需要获取来自其他地方的 XML 字符串或创建 xml.xml 文件并保存你的 XML 以便上面的代码工作
我如何定义一个对象来反序列化以下 XML:
<body>
<S1 A="1">
<S2 B="1">
<S3 C="1"/>
<S3 C="1"/>
</S2>
<S2 B="2"/>
</S1>
<S1 A="2"/>
我强烈建议使用 xsd.exe,这有助于从 XDR、XML 和 XML 中生成 XML 模式或公共语言运行时 classes =35=] 文件,或来自运行时程序集中的 classes。
- 打开VS Developer Command Prompt
- 键入
xsd.exe PathToXmlFile.xml /outputdir:OutputDir
并按Enter
- 这将生成*.xsd
文件 - 键入
xsd.exe PreviouslyCreatedXsdFile.xsd /classes /outputdir:OutputDir
并按Enter
- 这将生成*.cs
文件(class 定义)。
就这些了!
试试吧!
试试这个....
使用.....
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
类.......
[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; }
}
代码......
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;
}
你的XML.......
<body>
<S1 A="1">
<S2 B="1">
<S3 C="1"/>
<S3 C="1"/>
</S2>
<S2 B="2"/>
</S1>
<S1 A="2"/>
</body>
我添加了结束标记.....我正在将您的 XML 读入应用程序构建文件夹中名为 xml.xml 的文件中的字符串...您需要获取来自其他地方的 XML 字符串或创建 xml.xml 文件并保存你的 XML 以便上面的代码工作