CaffGeek
CaffGeek

Reputation: 22054

.Net Xml Deserialization

I have the following XElement

  <Issue Type="Duplicate" Distance="1">
    <Record>
      <ID>6832</ID>
      <Name_First>JAMES </Name_First>
      <Name_Last>SMITH</Name_Last>
      <Company>SMITH CO.</Company>
    </Record>
    <Record>
      <ID>6831</ID>
      <Name_First>JAMES</Name_First>
      <Name_Last>SMITH</Name_Last>
      <Company>SMITH CO.</Company>
    </Record>
  </Issue>

I'm trying to Deserialize it into this object

public class Issue
{
    [XmlAttribute]
    public string Type { get; set; }

    [XmlArrayItem(typeof(XElement), ElementName = "Record")]
    public List<XElement> Record { get; set; }
}

The type works no problem, but I can't get the two Record nodes into the Record list of the object.

Is it possible without overriding ISerializable and writing custom code?

Upvotes: 1

Views: 392

Answers (2)

Simon Steele
Simon Steele

Reputation: 11608

Try this:

public class Issue 
{
    [XmlAttribute]
    public string Type { get; set; }

    [XmlAnyElement("Record")]
    public List<XElement> Record { get; set; }
}

I think that tells the serializer that multiple Record elements will go in the list.

Upvotes: 2

Numenor
Numenor

Reputation: 1706

Implement Record class which has ID, Name_First, Name_Last and Company fields

Upvotes: 1

Related Questions