[英]Implementing IXmlSerializable on a collection object
I have an xml file looking somewhat like this: 我有一个看起来像这样的xml文件:
<xml>
<A>value</A>
<B>value</B>
<listitems>
<item>
<C>value</C>
<D>value</D>
</item>
</listitems>
</xml>
And I have a two objects representing this xml: 我有两个代表这个xml的对象:
class XmlObject
{
public string A { get; set; }
public string B { get; set; }
List<Item> listitems { get; set; }
}
class Item : IXmlSerializable
{
public string C { get; set; }
public string D { get; set; }
//Implemented IXmlSerializeable read/write
public void ReadXml(System.Xml.XmlReader reader)
{
this.C = reader.ReadElementString();
this.D = reader.ReadElementString();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("C", this.C);
writer.WriteElementString("D", this.D);
}
}
I use the XmlSerializer to serialize/deserialize the XmlObject to file. 我使用XmlSerializer将XmlObject序列化/反序列化为文件。
The problem is that when I implemented the custom IXmlSerializable functions on my "sub-object" Item I always only get one item(the first) in my XmlObject.listitems collection when deserializing the file. 问题是,当我在我的“子对象”项目上实现自定义IXmlSerializable函数时,在反序列化文件时,我总是只在我的XmlObject.listitems集合中获得一个项目(第一个)。 If I remove the : IXmlSerializable everything works as expected.
如果我删除:IXmlSerializable一切都按预期工作。
What do I do wrong? 我做错了什么?
Edit: I have have implemented IXmlSerializable.GetSchema and I need to use IXmlSerializable on my "child-object" for doing some custom value transformation. 编辑:我已经实现了IXmlSerializable.GetSchema,我需要在我的“子对象”上使用IXmlSerializable进行一些自定义值转换。
Modify your code like this: 像这样修改你的代码:
public void ReadXml(System.Xml.XmlReader reader)
{
reader.Read();
this.C = reader.ReadElementString();
this.D = reader.ReadElementString();
reader.Read();
}
First you skip the start of the Item node, read the two strings, then read past the end node so the reader is at the correct place. 首先,您跳过Item节点的开头,读取两个字符串,然后读取结束节点,以便读取器位于正确的位置。 This will read all nodes in the array.
这将读取数组中的所有节点。
You need to pay attention when modifying xml yourself :) 你自己修改xml时需要注意:)
You don't need to use IXmlSerializable. 您不需要使用IXmlSerializable。 But if you want you should implement GetShema() method.
但是如果你想要你应该实现GetShema()方法。 After some modification code that works looks like that:
一些修改后的代码看起来像这样:
[XmlRoot("XmlObject")]
public class XmlObject
{
[XmlElement("A")]
public string A { get; set; }
[XmlElement("B")]
public string B { get; set; }
[XmlElement("listitems")]
public List<Item> listitems { get; set; }
}
public class Item : IXmlSerializable
{
[XmlElement("C")]
public string C { get; set; }
[XmlElement("D")]
public string D { get; set; }
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
this.C = reader.ReadElementString();
this.D = reader.ReadElementString();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("C", this.C);
writer.WriteElementString("D", this.D);
}
#endregion
}
Results for 2 items in itemlist will look like that: itemlist中的2个项目的结果将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<XmlObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<A>value</A>
<B>value</B>
<listitems>
<C>value0</C>
<D>value0</D>
</listitems>
<listitems>
<C>value1</C>
<D>value1</D>
</listitems>
</XmlObject>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.