简体   繁体   English

使用XmlSerializer重命名平面xml数组中的数组项

[英]Renaming array items in a flat xml array using an XmlSerializer

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将<Item>元素读取为MyClass类的对象。

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. 由于元素名称<Item>与类名称MyCLass不同,因此数组中的元素完全不会反序列化。 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. 如果我将MyClass重命名为Item ,则上面的代码有效,但不幸的是,我不允许更改XML文件或类名。

How do I go about mapping the two so that the file can be read correctly? 我该如何映射两者,以便可以正确读取文件?

Thanks in advance! 提前致谢!

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();
}

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. LINQ to XML使其非常简单。 For example, in this case: 例如,在这种情况下:

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. 诚然,如果您需要序列化具有复杂关系的对象(两个对象指向同一个孩子或其他对象),这将变得很繁琐,但是我在过去已经取得了很大的成功。

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

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