代码之家  ›  专栏  ›  技术社区  ›  Jesper Palm

在集合对象上实现IXML可序列化

  •  5
  • Jesper Palm  · 技术社区  · 15 年前

    我有一个XML文件,看起来有点像这样:

    <xml>
      <A>value</A>
      <B>value</B>
      <listitems>
        <item>
          <C>value</C>
          <D>value</D> 
        </item>
      </listitems>
    </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);
      }
    }
    

    我使用XML序列化程序将XML对象序列化/反序列化为文件。

    问题是,当我在“子对象”项上实现自定义的IXML可序列化函数时,在反序列化文件时,总是在xmlObject.listems集合中只获取一个项(第一个)。 如果我删除:ixmlserializable,一切都将按预期工作。

    我做错了什么?

    编辑:我已经实现了IXMLSerializable.getSchema,我需要在我的“子对象”上使用IXMLSerializable来进行一些自定义值转换。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Mikael Svenson    15 年前

    像这样修改代码:

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.Read();
            this.C = reader.ReadElementString();
            this.D = reader.ReadElementString();
            reader.Read();
        }
    

    首先跳过item节点的开始部分,读取两个字符串,然后读取结束节点,这样读卡器就位于正确的位置。这将读取数组中的所有节点。

    在自己修改XML时需要注意:)

        2
  •  1
  •   kyrisu    15 年前

    您不需要使用IXML可序列化。但如果需要,您应该实现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
    }
    

    项目列表中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>