C# 在集合对象上实现IXmlSerializable
我有一个xml文件,看起来有点像这样:C# 在集合对象上实现IXmlSerializable,c#,xml-serialization,C#,Xml Serialization,我有一个xml文件,看起来有点像这样: <xml> <A>value</A> <B>value</B> <listitems> <item> <C>value</C> <D>value</D> </item> </listitems> </xml> public
<xml>
<A>value</A>
<B>value</B>
<listitems>
<item>
<C>value</C>
<D>value</D>
</item>
</listitems>
</xml>
public void ReadXml(System.Xml.XmlReader reader)
{
reader.Read();
this.C = reader.ReadElementString();
this.D = reader.ReadElementString();
reader.Read();
}
价值
价值
价值
价值
我有两个对象表示这个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);
}
}
类XmlObject
{
公共字符串A{get;set;}
公共字符串B{get;set;}
列出listitems{get;set;}
}
类项:IXmlSerializable
{
公共字符串C{get;set;}
公共字符串D{get;set;}
//实现的IXMLSerialized读/写
public void ReadXml(System.Xml.XmlReader)
{
this.C=reader.ReadElementString();
this.D=reader.ReadElementString();
}
public void WriteXml(System.Xml.XmlWriter)
{
WriteElementString(“C”,this.C);
WriteElementString(“D”,this.D);
}
}
我使用XmlSerializer将XmlObject序列化/反序列化为文件
问题是,当我在我的“子对象”项上实现自定义IXmlSerializable函数时,在反序列化文件时,我总是在XmlObject.listitems集合中只得到一个项(第一个)。
如果我删除:IXmlSerializable,一切都会正常工作
我做错了什么
编辑:我已经实现了IXmlSerializable.GetSchema,我需要在我的“子对象”上使用IXmlSerializable进行一些自定义值转换。您不需要使用IXmlSerializable。但如果需要,应该实现GetShema()方法。经过一些修改后,代码如下所示:
[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
}
<?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>
[XmlRoot(“XmlObject”)]
公共类XmlObject
{
[XmlElement(“A”)]
公共字符串A{get;set;}
[XmlElement(“B”)]
公共字符串B{get;set;}
[XmlElement(“列表项”)]
公共列表列表项{get;set;}
}
公共类项:IXmlSerializable
{
[XmlElement(“C”)]
公共字符串C{get;set;}
[XmlElement(“D”)]
公共字符串D{get;set;}
#区域IXmlSerializable成员
public System.Xml.Schema.XmlSchema GetSchema()
{
抛出新的NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader)
{
this.C=reader.ReadElementString();
this.D=reader.ReadElementString();
}
public void WriteXml(System.Xml.XmlWriter)
{
WriteElementString(“C”,this.C);
WriteElementString(“D”,this.D);
}
#端区
}
itemlist中2项的结果如下所示:
[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
}
<?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>
价值
价值
价值0
价值0
价值1
价值1
如下修改代码:
<xml>
<A>value</A>
<B>value</B>
<listitems>
<item>
<C>value</C>
<D>value</D>
</item>
</listitems>
</xml>
public void ReadXml(System.Xml.XmlReader reader)
{
reader.Read();
this.C = reader.ReadElementString();
this.D = reader.ReadElementString();
reader.Read();
}
首先跳过Item节点的开头,读取两个字符串,然后读取结束节点,以便读取器位于正确的位置。这将读取阵列中的所有节点
您自己修改xml时需要注意:)