I have an XML file with the following format:
我有一个XML文件,格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item Property1="A" Property2="B" />
<Item Property1="C" Property2="D" />
</Items>
I need to read the <Item>
elements as objects of class MyClass
using an XmlSerializer.
我需要使用XmlSerializer将
public class MyCLass
{
[XmlAttribute]
public string Property1 { get; set; }
[XmlAttribute]
public string Property2 { get; set; }
}
Currently, I have the following code to read the file:
目前,我有以下代码来读取文件:
XmlSerializer serializer =
new XmlSerializer(typeof(MyClass[]), new XmlRootAttribute(@"Items"));
MyClass[] list = (MyClass[])serializer.Deserialize(...);
Since the element name <Item>
is different from the class name MyCLass
, the elements in the array are not deserialized at all. The above code works if I rename MyClass
to Item
, but unfortunately I am not allowed to change the XML file or the class names.
由于元素名
How do I go about mapping the two so that the file can be read correctly?
如何映射这两个文件,以便正确读取文件?
Thanks in advance!
提前谢谢!
2 个解决方案
#1
4
Use a wrapper class that contains the array, this will allow you to apply the XmlElement
attribute:
使用包含数组的包装器类,这将允许您应用XmlElement属性:
public class MyClassList
{
[XmlElement("Item")]
public MyClass[] Items { get; set; }
}
var items = new[]
{
new MyClass { Property1 = "A", Property2 = "B" },
new MyClass { Property1 = "C", Property2 = "D" },
};
var list = new MyClassList { Items = items };
using (var writer = new StringWriter())
{
var xs = new XmlSerializer(typeof(MyClassList), new XmlRootAttribute("Items"));
xs.Serialize(writer, list);
writer.ToString().Dump();
}
#2
2
Personally I would serialize and deserialize manually - I've found that it's easier to get whatever flexibility you want that way rather than spending a long time messing around with the built-in serialization and living with the various restrictions it imposes. LINQ to XML makes it pretty simple. For example, in this case:
就我个人而言,我将手工序列化和反序列化——我发现,这样更容易获得所需的灵活性,而不是花很长时间处理内置的序列化和反序列化所带来的各种限制。LINQ to XML使它非常简单。例如,在这种情况下:
XDocument doc = XDocument.Load("test.xml");
// You could use an array if you really wanted, of course.
List<MyClass> list = doc.Root
.Elements("Item")
.Select(x => new MyClass {
Property1 = (string) x.Attribute("Property1"),
Property2 = (string) x.Attribute("Property2"),
})
.ToList();
Admittedly this will get hairy if you need to serialize objects with complicated relationships (two objects referring to the same child or whatever) but I've had a great deal of success with it in the past.
不可否认,如果您需要序列化具有复杂关系的对象(两个对象引用相同的子对象或其他对象),那么这将非常麻烦,但是我过去在这方面已经取得了很大的成功。
#1
4
Use a wrapper class that contains the array, this will allow you to apply the XmlElement
attribute:
使用包含数组的包装器类,这将允许您应用XmlElement属性:
public class MyClassList
{
[XmlElement("Item")]
public MyClass[] Items { get; set; }
}
var items = new[]
{
new MyClass { Property1 = "A", Property2 = "B" },
new MyClass { Property1 = "C", Property2 = "D" },
};
var list = new MyClassList { Items = items };
using (var writer = new StringWriter())
{
var xs = new XmlSerializer(typeof(MyClassList), new XmlRootAttribute("Items"));
xs.Serialize(writer, list);
writer.ToString().Dump();
}
#2
2
Personally I would serialize and deserialize manually - I've found that it's easier to get whatever flexibility you want that way rather than spending a long time messing around with the built-in serialization and living with the various restrictions it imposes. LINQ to XML makes it pretty simple. For example, in this case:
就我个人而言,我将手工序列化和反序列化——我发现,这样更容易获得所需的灵活性,而不是花很长时间处理内置的序列化和反序列化所带来的各种限制。LINQ to XML使它非常简单。例如,在这种情况下:
XDocument doc = XDocument.Load("test.xml");
// You could use an array if you really wanted, of course.
List<MyClass> list = doc.Root
.Elements("Item")
.Select(x => new MyClass {
Property1 = (string) x.Attribute("Property1"),
Property2 = (string) x.Attribute("Property2"),
})
.ToList();
Admittedly this will get hairy if you need to serialize objects with complicated relationships (two objects referring to the same child or whatever) but I've had a great deal of success with it in the past.
不可否认,如果您需要序列化具有复杂关系的对象(两个对象引用相同的子对象或其他对象),那么这将非常麻烦,但是我过去在这方面已经取得了很大的成功。