简体   繁体   English

将XML反序列化为C#对象

[英]Deserialize XML into C# object

I need to read XML and deserialize it into an object in C#. 我需要阅读XML并将其反序列化为C#中的对象。 I have the following XML: 我有以下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>

Here is my code to deserialize it: 这是我的反序列化代码:

    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

Deserialize does not read Component. 反序列化不读取组件。 Package.Applications.Component is always empty. Package.Applications.Component始终为空。 I have the values for all the rest. 我拥有其余所有的价值。 It looks like the definition of my object must be wrong somewhere. 看起来我的对象的定义在某处一定是错误的。 What's wrong in my definition? 我的定义有什么问题?

The problem is how you declared the Component property: 问题是如何声明Component属性:

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

With this code, the serializer expects XML like this: 使用此代码,序列化程序期望XML如下所示:

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

The correct way to declare it to match your XML is like this: 声明它与XML匹配的正确方法是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM