将XML反序列化为C#对象
问题描述:
我需要读取XML并将其反序列化为C#中的对象。 我有以下XML:将XML反序列化为C#对象
<?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始终为空。我有其余的价值。它看起来像我的对象的定义一定是错的地方。 我的定义有什么问题?
答
问题是你如何宣布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; }
解决此类问题的一种方法是序列化您的对象并比较X ML。比你要么看到差异或能够产生更小的样本。 – 2014-10-05 20:19:18
简化问题。那里有很多代码。 – 2014-10-05 20:21:01