我要得到这样的XML
<?xml version="1.0"?>
<Topic>
  <AnswerList>
    <Solution>
        <Device DeviceId="abc7" DeviceName="ttt7" />
        <Device DeviceId="abc22" DeviceName="ttt33"/>
    </Solution>
    <Solution>
        <Device DeviceId="abc7" DeviceName="ttt7" />
        <Device DeviceId="abc22" DeviceName="ttt33"  />
    </Solution>
  </AnswerList>
</Topic>定义的类为。
class Topic
    {
        public List<Solution> AnswerList { get; set; }
    }class Solution
    {
        public List<Device> DeviceList { get; set; }
    }    [Serializable]
    class Device
    {
        [XmlAttribute(AttributeName="DeviceId")]
        public string DeviceId { get; set; }        [XmlAttribute(AttributeName = "DeviceName")]
        public string DeviceName { get; set; }
可是我得到的结果总是
<?xml version="1.0"?>
<Topic>
  <AnswerList>
    <Solution>
      <DeviceList>
        <Device DeviceId="abc7" DeviceName="ttt7" />
        <Device DeviceId="abc22" DeviceName="ttt33"/>
      </DeviceList>
    </Solution>
    <Solution>
      <DeviceList>
        <Device DeviceId="abc7" DeviceName="ttt7" />
        <Device DeviceId="abc22" DeviceName="ttt33"  />
      </DeviceList>
    </Solution>
  </AnswerList>
</Topic>
这两个节点<Solution>和 <DeviceList>我只想要一个。我改怎么样定义我的类。

解决方案 »

  1.   

    Solution是类名自动生成的。我也是想去掉。
      

  2.   

    蛋疼的要求,好好的序列化不用,非要自己写一套.如果Solution类可以修改,可以这么处理
     public class Solution : IXmlSerializable
        {           
            [XmlIgnore]
            public List<Device> DeviceList { get; set; }        public System.Xml.Schema.XmlSchema GetSchema()
            {
                return null;
            }        public void ReadXml(XmlReader reader)
            {
                //todo...
            }        public void WriteXml(XmlWriter writer)
            {
                foreach (var dev in DeviceList)
                {
                    writer.WriteStartElement("Device");
                    writer.WriteAttributeString("DeviceId", dev.DeviceId);
                    writer.WriteAttributeString("DeviceName", dev.DeviceName);
                    writer.WriteEndElement();
                }
            }
        }
      

  3.   

    试试
    [XmlArray("Device"), XmlArrayItem(typeof(Device))]
    public List<Device> DeviceList { get; set; }
      

  4.   

    http://topic.csdn.net/u/20110708/16/90e0a3a8-e237-4e01-afde-c4b1fd5617b6.html?34718
      

  5.   

    [XmlArray("Device"), XmlArrayItem(typeof(Device))] public List<Device> DeviceList { get; set; }这样没任何变化
    [XmlIgnore],子节点都没有了。感谢huangwenquan123,感谢大家。
      

  6.   

    [XmlElement("Device")]
    public List<Device> DeviceList { get; set; }
    这样的
     
      

  7.   

    是否有检查过Topic类中的AnswerList有几个
      

  8.   

    class Topic
      {
      public AnswerList  AnswerList { get; set; }
      }class AnswerList :
      {
      public List<Device> DeviceList { get; set; }
      }  [Serializable]
      class Device
      {
      [XmlAttribute(AttributeName="DeviceId")]
      public string DeviceId { get; set; }  [XmlAttribute(AttributeName = "DeviceName")]
      public string DeviceName { get; set; }