在xml C#中反序列化数组

时间:2016-04-08 13:05:51

标签: c# xml serialization

我有这个Xml

<Facility ID="353">
    <Name>Test</Name>
    <Buildingtype>Test</Buildingtype>
    <SMSInfoID>
      <ID default="True">140</ID>
      <ID default="True">140</ID>
      <ID default="True">140</ID>
    </SMSInfoID>
  </Facility>

我有使用标签的问题

我已经尝试了很多方法来解决它,它是我测试的最后一个但仍无法到达此元素内的数据。

public class Facility
    {     
        [XmlAttribute("ID"), Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Id { get; set; }

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

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

        [XmlElement("SMSInfoID")]
        public virtual SMSInfoID SMSInfoID { get; set; }       
    }

[XmlRoot("SMSInfoID")]
    public class SMSInfoID
    {
        [XmlIgnore, Key]
        public int Id { get; set; }

        [XmlElement("ID")]
        public  List<ID> ID { get; set; }        
    }



[XmlRoot("ID")]
    public  class ID
    {
        [Key]
        public int Id { get; set; }
        [XmlAttribute("default")]
        public string Default { get; set; }
    }

任何人都可以帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

如果您没有使用SMSInfoID的ID,您可以将其重写为Facility的属性:

[XmlArray("SMSInfoID")]
[XmlArrayItem("ID")]
public List<ID> ID { get; set; } 

或将此属性包装为XML:

[XmlArray("SMSInfoID")]
[XmlArrayItem("ID")]
public List<ID> ID 
{ 
    get {return SMSInfoID.ID;} 
    set {SMSInfoID.ID = value; 
} 

[XmlIgnore]
public virtual SMSInfoID SMSInfoID { get; set; } 

如果不可能,你可以改变什么?

答案 1 :(得分:0)

不要在您的XML中使用阵列。该阵列添加了额外级别的标签。 下面的代码与您匹配xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Facility facility = new Facility() {
                Id = 353,
                Name = "Test",
                CategoryID = "Test",
                SMSInfoID = new SMSInfoID() {
                        ID = new List<ID>() {
                            new ID() { _default = true, value = 140},
                            new ID() { _default = true, value = 140},
                            new ID() { _default = true, value = 140}
                        }
                    }
                };

            XmlSerializer serializer = new XmlSerializer(typeof(Facility));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, facility);
            writer.Flush();
            writer.Close();
            writer.Dispose();

        }
    }
    public class Facility
    {
        [XmlAttribute("ID")]
        public int Id { get; set; }

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

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

        [XmlElement("SMSInfoID")]
        public virtual SMSInfoID SMSInfoID { get; set; }
    }

    [XmlRoot("SMSInfoID")]
    public class SMSInfoID
    {

        [XmlElement("ID")]
        public List<ID> ID { get; set; }
    }
    [XmlRoot("ID")]
    public class ID
    {
        [XmlAttribute("default")]
        public Boolean _default {get; set;}
        [XmlText()]
        public int value { get; set; }
    }
}

答案 2 :(得分:0)

向属性XmlText添加属性Id到您的班级ID

[XmlRoot("ID")]
public class ID
{
    [XmlText, Key] // <-- here
    public int Id { get; set; }
    [XmlAttribute("default")]
    public string Default { get; set; }
}