需要xml重复节点帮助

时间:2011-07-04 10:36:41

标签: c# xml xml-serialization

我需要在XML中复制以下内容,但不确定如何执行此操作:

<FAMILY>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</FAMILY>

我正在使用一个非常基本的例子来解释我需要帮助的内容。 XML是通过解析可序列化类生成的:

[Serializable]
[XmlRoot("FAMILY")]
public class FamilyBlock
{
public string NAME { get; set; }
public int AGE { get; set; }
public DateTime? DOB { get; set; }

 public FamilyBlock(string name, int age, DateTime? dob)
{
 NAME = name;
 AGE = age;
 DOB = dob;
}
}

我尝试用列表对象解决问题,但我得到以下内容(添加了对象名称 - 我不需要)。

<FAMILY>
<MEMBER>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</MEMBER>
<MEMBER>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</MEMBER>
<MEMBER>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</MEMBER>
<MEMBER>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</MEMBER>
</FAMILY>

我确定这是一个简单的问题,但我真的不太了解xml

3 个答案:

答案 0 :(得分:1)

[XmlRoot("Family")]
public class FamilyBlock
{
    [XmlElement("NAME")]
    public string[] NAME { get; set; }
    [XmlElement("AGE")]
    public int[] AGE { get; set; }
    [XmlElement("DOB")]
    public DateTime?[] DOB { get; set; }
}

xml序列化后看起来像:

<?xml version="1.0"?>
<Family xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <NAME>a</NAME>
  <NAME>s</NAME>
  <AGE>1</AGE>
  <AGE>3</AGE>
  <DOB>2011-07-04T13:51:20.6757286+03:00</DOB>
  <DOB xsi:nil="true" />
</Family>

答案 1 :(得分:0)

好的,所以我在Nagg的帮助下做的是实现IXmlSerializable接口。

[Serializable]
[XmlRoot("FAMILY")]
public class FamilyBlock : IXmlSerializable
{
   [XmlElement("NAME")]
   public List<string> NAME { get; set; }
   [XmlElement("AGE")]
   public List<int> AGE { get; set; }
   [XmlElement("DOB")]
   public List<DateTime?> DOB { get; set; }

   public FamilyBlock(string name, int age, DateTime? dob)
   {
      NAME = name;
      AGE = age;
      DOB = dob;
   }

   public void WriteXml(XmlWriter writer)
   {
      for (int i = 0; i < this.NAME.Count; i++)
      {
          writer.WriteElementString("NAME ", this.NAME[i]);
          writer.WriteElementString("AGE", this.AGE[i]);
          writer.WriteElementString("DOB", this.DOB[i]);
      }

   }
}

如果不是正确的方法,请随时更新,但这对我有用。请注意,以下方法也是继承的:

public XmlSchema GetSchema()
{
    throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
    throw new NotImplementedException();
}

结果:

<FAMILY>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
 <NAME>
 <AGE>
 <DATEOFBIRTH>
</FAMILY>

答案 2 :(得分:0)

另外,我稍微重构了一下。我创建了名为FamilyMember的类:

public class FamilyMember
{
    public FamilyMember()
    {

    }
    public FamilyMember(string name, string age, string dob)
    {
        NAME =name;
        // etc etc
    }

    [XmlElement("NAME")]
    public string NAME { get; set; }

    [XmlElement("AGE")]
    public string AGE { get; set; }

    [XmlElement("DOB")]
    public string DOB { get; set; }
    }
}

所以我想要序列化的XML类现在看起来像:

[Serializable]
[XmlRoot("RESY")]
public class FamilyBlock: IXmlSerializable
{
    public List<FamilyMember> FAMILYMEMBERS{ get; set; }

    public FamilyBlock()
    {

    }

    public FamilyBlock(string name, int age, DateTime? dob)
    {
        var familyMembers = new List<FamilyMember>  // etc etc
     ....
    }

    public void WriteXml(XmlWriter writer)
    {

        foreach (var item in FAMILYMEMBERS)
        {
            writer.WriteElementString("NAME", item.NAME);
            writer.WriteElementString("AGE", item.AGE);
            writer.WriteElementString("DOB", item.DOB);
        }
    }
}