I'm trying to deserialize some C# documentation XML, starting at the members
node in the snippet below.
<?xml version="1.0"?>
<doc>
<assembly>
<name>Licsp</name>
</assembly>
<members>
<member name="T:Licsp.Parsing.SExp">
<summary>
Intermediary structure used by parser
</summary>
</member>
</members>
</doc>
Each member
should deserialize to a DocItem
; the collection of members
should deserialize to DocFile
.
[XmlRoot("members")]
public class DocFile
{
[XmlArrayItem("member", typeof(DocItem))]
public List<DocItem> Items = new List<DocItem>();
}
[Serializable()]
public class DocItem
{
[XmlAttribute("name")]
public string Name;
[XmlElement("summary")]
public string Summary;
[XmlElement("example")]
public string Example;
[XmlElement("remarks")]
public string Remarks;
}
Here's the calling code:
using (var reader = new XmlTextReader(documentation))
{
reader.ReadToDescendant("members");
var deserializer = new XmlSerializer(typeof(DocFile));
DocFile helpfile = (DocFile)deserializer.Deserialize(reader.ReadSubtree());
}
I'm getting back a DocFile
object, but Items
is empty. Serializing a DocFile
object produces the following:
<?xml version="1.0" encoding="utf-8"?>
<members xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<member name="name">
<summary>summary</summary>
<example>example</example>
<remarks>remarks</remarks>
</member>
</Items>
</members>
I'm not sure why the deserialization is failing. What am I doing wrong?
Since members
is your root and you need the member
children in a collection, replace your [XmlArrayItem]
with [XmlElement]
XmlElement
can be used for individual nodes and also to roll many nodes with the same name into a collection.
[XmlRoot("members")]
public class DocFile
{
[XmlElement("member")]
public List<DocItem> Items = new List<DocItem>();
}
XmlArrayItem
is to be used in conjunction with XmlArray
. This would have worked if for example, your were one level up with doc
as your root.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.