[英]XML Deserialization
I have the following xml file. 我有以下xml文件。
<a>
<b>
<c>val1</c>
<d>val2</d>
</b>
<b>
<c>val3</c>
<d>val4</d>
</b>
<a>
I want to deserialize this into a class and I want to access them with the objects of the class created. 我想将它反序列化为一个类,我想用创建的类的对象访问它们。 I am using C#.
我正在使用C#。 I am able to deserialize and get the value into the object of class '
a
' (the <a>
tag). 我能够反序列化并将值放入类'
a
'( <a>
标签)的对象中。 but how to access the value of <b>
from this object? 但如何从这个对象访问
<b>
的值? I did the following coding: 我做了以下编码:
[Serializable()] [XmlRoot("a")] public class a { [XmlArray("a")] [XmlArrayItem("b", typeof(b))] public b[] bb{ get; set; } } [Serializable()] public class b { [XmlElement("c")] public string c{ get; set; } [XmlElement("d")] public string d{ get; set; } } class Program { static void Main(string[] args) { a i = null; string path = "test.xml"; XmlSerializer serializer = new XmlSerializer(typeof(a)); StreamReader reader = new StreamReader(path); i = (a)serializer.Deserialize(reader); reader.Close(); //i want to print all b tags here Console.Read(); } }
For this to work you can make the following change 为此,您可以进行以下更改
public class a
{
[XmlElement("b")]
public b[] bb{ get; set; }
}
By using the XmlElement
attribute on the array, you are essentially telling the serializer that the array elements should be serialize/deserialized as direct child elements of the current element. 通过在数组上使用
XmlElement
属性,您实际上是在告诉序列化程序,数组元素应该序列化/反序列化为当前元素的直接子元素。
Here is a working example, I put the XML in a string just to make the example self contained. 这是一个工作示例,我将XML放在一个字符串中只是为了使示例自包含。
using System;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
@"<a>
<b>
<c>val1</c>
<d>val2</d>
</b>
<b>
<c>val3</c>
<d>val4</d>
</b>
</a>";
XmlSerializer xs = new XmlSerializer(typeof(a));
a i = (a)xs.Deserialize(new StringReader(xml));
if (i != null && i.bb != null && i.bb.Length > 0)
{
Console.WriteLine(i.bb[0].c);
}
else
{
Console.WriteLine("Something went wrong!");
}
Console.ReadKey();
}
}
[XmlRoot("a")]
public class a
{
[XmlElement("b")]
public b[] bb { get; set; }
}
public class b
{
[XmlElement("c")]
public string c { get; set; }
[XmlElement("d")]
public string d { get; set; }
}
}
When in doubt with creating your xml serialization classes, i find the easiest way to solve the problem is to: 如果对创建xml序列化类有疑问,我发现解决问题的最简单方法是:
i wrote a quick tutorial on it in a blog post a while ago: http://www.diaryofaninja.com/blog/2010/05/07/make-your-xml-stronglytyped-because-you-can-and-its-easy 我刚才在博客文章中写了一篇快速教程: http : //www.diaryofaninja.com/blog/2010/05/07/make-your-xml-stronglytyped-because-you-can-and-its -简单
it takes less than a minute and you can then easily tweak things from there. 它需要不到一分钟,然后你可以轻松调整那里的东西。 XSD.exe is your friend
XSD.exe是你的朋友
I find the easiest way to solve the problem is to change your definition of classes a
and b
to the following 我发现解决问题的最简单方法是将类
a
和b
定义更改为以下内容
public class b {
public string c { get; set; }
public string d { get; set; }
}
[XmlRoot(Namespace="", ElementName="a")]
public class a : List<b> { }
then your program will work. 然后你的程序将工作。 Optionally you can add to the
class b
the attribute [XmlRoot (Namespace = "", ElementName = "b")]
(可选)您可以向
class b
添加属性[XmlRoot (Namespace = "", ElementName = "b")]
class Program
{
static void Main(string[] args)
{
string employeedata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><name>test</bar></nmae>";//demo xml data
using (TextReader sr = new StringReader(employeedata))
{
XmlSerializer serializer = new XmlSerializer(typeof(Employee));//pass type name in XmlSerializer constructor here
Employee response = (Employee)serializer.Deserialize(sr);
Console.WriteLine(response.name);
}
}
}
[System.Xml.Serialization.XmlRoot("tag")]
public class Employee
{
public string name { get; set; }
}
Change if block (from Chris Tyler's answer) like below. 如果阻止(来自Chris Tyler的答案)改变如下。
if (i != null && i.bb != null && i.bb.Length > 0)
{
foreach (b t in i.bb)
{
Console.WriteLine(t.c);
Console.WriteLine(t.d);
}
}
then it will give you below result: 然后它会给你以下结果:
val1 val2 val3 val4 val1 val2 val3 val4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.