我遇到过一些非常奇怪的事情,如果有人能够解释这里发生的事情,我会很高兴。
这里的代码非常合适:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp1
{
[Serializable]
[XmlRoot(ElementName = "Access")]
public class Access
{
public Access()
{
}
[XmlElement(ElementName = "Else")]
public string Else { get; set; }
[XmlElement(ElementName = "Element")]
public string Element { get; set; }
[XmlArray(ElementName = "fields")]
public Field[] Fields { get; set; }
}
[Serializable]
[XmlRoot(ElementName = "Field")]
public class Field
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Type")]
public string Type { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var data = @"<Access>
<Element>what is this ?!</Element>
<Else>432432elseasdas123213</Else>
<fields>
<Field><Name>Skype</Name><Type>Talking</Type></Field>
<Field><Name>IP Phone</Name><Type>Talking</Type></Field>
<Field><Name>PC</Name><Type>Still</Type></Field>
<Field><Name>Laptop</Name><Type>Mobile</Type></Field>
</fields>
</Access>";
var serializer = new XmlSerializer(typeof(Access));
Access access;
using (var stream = new StringReader(data))
using (var reader = XmlReader.Create(stream))
{
access = (Access)serializer.Deserialize(reader);
Console.WriteLine("Fields");
foreach (Field item in access.Fields)
{
Console.WriteLine(item.Name);
Console.WriteLine(item.Type);
}
}
}
}
}
和这里的代码(几乎完全相同)但不能按预期工作:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp1
{
[Serializable]
[XmlRoot(ElementName = "Access")]
public class Access
{
public Access()
{
}
[XmlElement(ElementName = "Else")]
public string Else { get; set; }
[XmlElement(ElementName = "Element")]
public string Element { get; set; }
[XmlArray(ElementName = "fields")]
public Field[] Fields { get; set; }
}
[Serializable]
[XmlRoot(ElementName = "field")]
public class Field
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Type")]
public string Type { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var data = @"<Access>
<Element>what is this ?!</Element>
<Else>432432elseasdas123213</Else>
<fields>
<field><Name>Skype</Name><Type>Talking</Type></field>
<field><Name>IP Phone</Name><Type>Talking</Type></field>
<field><Name>PC</Name><Type>Still</Type></field>
<field><Name>Laptop</Name><Type>Mobile</Type></field>
</fields>
</Access>";
var serializer = new XmlSerializer(typeof(Access));
Access access;
using (var stream = new StringReader(data))
using (var reader = XmlReader.Create(stream))
{
access = (Access)serializer.Deserialize(reader);
Console.WriteLine("Fields");
foreach (Field item in access.Fields)
{
Console.WriteLine(item.Name);
Console.WriteLine(item.Type);
}
}
}
}
}
这两段代码之间的唯一区别是Field类上ElementName的大写,以及此类引用的XML中元素的大写。
我宁愿不改变XML中的大小写以匹配ElementName。
感谢。