Reputation: 643
I need to read XML and deserialize it into an object in C#. I have the following 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. 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?
Upvotes: 1
Views: 231
Reputation: 292425
The problem is how you declared the Component
property:
[XmlArray("Component")]
[XmlArrayItem("Component")]
public List<Component> Component { get; set; }
With this code, the serializer expects XML like this:
<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:
[XmlElement("Component")]
public List<Component> Components { get; set; }
Upvotes: 2