将XML反序列化为C#对象

时间:2014-10-05 20:15:58

标签: c# xml deserialization xml-deserialization

我需要读取XML并将其反序列化为C#中的对象。 我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<Package>
  <Info>
    <Info Name="Rx.y" Description="test app" />
  </Info>
  <Applications>
    <Application Name="MySoftware" Directory="S1">
      <Component Name="Web" Directory="Web" Version="1.0" />
      <Component Name="Database" Directory="SQL" Version="" />
    </Application>
  </Applications>
  <Tickets />
  <Targets>
    <Target Name="Dev" ID="1" />
    <Target Name="QA" ID="2" />
  </Targets>
  <Files>
    <File Name="S1\SQL\test.sql" Targets="1,2" ItemType="SQL" SortOrder="0" />
    <File Name="S1\SQL\file1.sql" Targets="1,2,12" Rename="||" ItemType="SQL" SortOrder="0" />
    <File Name="S1\SQL\file2.sql" Targets="1,2,12" Rename="||" ItemType="SQL" SortOrder="0" />
    <File Name="S1\Web\dir1\Test1.html" Targets="1,2,12" Rename="||" ItemType="HTML" SortOrder="" />
    <File Name="S1\SQL\PackLast.sql" Targets="1,2" ItemType="SQL" SortOrder="0" />
  </Files>
</Package>

这是我的反序列化代码:

    public class Info
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("Description")]
        public string Description { get; set; }
    }

    public class Component
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("Directory")]
        public string Directory { get; set; }
        [XmlAttribute("Version")]
        public string Version { get; set; }
        [XmlAttribute("Description")]
        public string Description { get; set; }
    }

    public class App
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("Directory")]
        public string Directory { get; set; }
        [XmlArray("Component")]
        [XmlArrayItem("Component")]
        public List<Component> Component { get; set; }
    }

    public class Target
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("ID")]
        public int ID { get; set; }
    }

    public class File
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("Targets")]
        public string Targets { get; set; }
        [XmlAttribute("ItemType")]
        public string ItemType { get; set; }
        [XmlAttribute("SortOrder")]
        public string SortOrder { get; set; }
    }

    [XmlRoot("Package")]
    public class Package
    {
        [XmlArray("Info")]
        [XmlArrayItem("Info")]
        public List<Info> Info { get; set; }
        [XmlArray("Applications")]
        [XmlArrayItem("Application")]
        public List<App> Applications { get; set; }
        [XmlArray("Targets")]
        [XmlArrayItem("Target")]
        public List<Target> Targets { get; set; }
        [XmlArray("Files")]
        [XmlArrayItem("File")]
        public List<File> Files { get; set; }
    }

   ms = new MemoryStream();
   pkg = new Package();

   file.Extract(ms);       // extract config.xml from package into memorystream
   ms.Position = 3;        // start reading the file at position 3

   pkg = (Package)reader.Deserialize(ms);  // read xml into object

反序列化不会读取组件。 Package.Applications.Component始终为空。我有其他所有的价值观。看起来我的对象的定义在某处肯定是错的。 我的定义有什么不对?

1 个答案:

答案 0 :(得分:2)

问题是您如何声明Component属性:

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

使用此代码,序列化程序需要这样的XML:

<Application Name="MySoftware" Directory="S1">
  <Component>
    <Component Name="Web" Directory="Web" Version="1.0" />
    <Component Name="Database" Directory="SQL" Version="" />
  </Component>
</Application>

声明它以匹配XML的正确方法如下:

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