views:

28

answers:

3

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?

+1  A: 

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

Numenor
I don't want a class, I want an XElement as the record could contain anything.
Chad
A: 

Should there be a

<Records>
</Records>

Tag around the records elements?

Shiraz Bhaiji
Preferably not.
Chad
+2  A: 

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.

Simon Steele
Then my `Record` property contains an `XElement`, but it contains only one `XElement` `<ID>6382</ID>` instead of two `Record` `XElement`s.
Chad
Changing that to `XmlAnyElement` seemed to work
Chad
Ah nice, thanks.
Simon Steele