0

I have a piece of XML that I need to serialize/deserialize but I'm having a lot of difficulty in getting it to work exactly as needed. The code I have works for 98% of the structure but I'm unable to get attributes serialized/deserialized on the root node.

Sample XML;

<rootNode date="2000-01-01" time="07:00">
    <detail>
        <reference>12345</reference>
    </detail>
</rootNode>

Classes currently being used to deserialize to;

[XmlType("detail")]
public class Detail
{
    [XmlElement("reference")]
    public string Reference { get; set; }
}

[XmlRoot("rootNode")]
public class Root : List<Detail>
{
    [XmlAttribute("date")]
    public string Date { get; set; }

    [XmlAttribute("time")]
    public string Time { get; set; }
}

The specifics of each element being renamed from the actual XML is important and necessary in my implementation. If I don't inherit from List<Detail> and have a collection property (assume Details of same type) then I can get the attributes but end up with XML like;

<rootNode date="2000-01-01" time="07:00">
    <Details>
        <detail>
            <reference>12345</reference>
        </detail>
    </Details>
</rootNode>

But that isn't what I'm trying to deserialize. Controlling the way it's serialized via XmlArray and XmlArrayItem allows me to rename the collection node and the item nodes but not get rid of the collection node itself. Any suggestions?

3 Answers 3

3

I think you should remove inheritance from Root to List, and add the Details attribute to Root:

[XmlType("detail")]
public class Detail
{
    [XmlElement("reference")]
    public string Reference { get; set; }
}

[XmlRoot("rootNode")]
public class Root
{
    [XmlAttribute("date")]
    public string Date { get; set; }

    [XmlAttribute("time")]
    public string Time { get; set; }

    [XmlElement("detail")]
    List<Detail> Details  { get; set; }

}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid that results in There was an error reflecting property 'Details'. from the XmlSerializer
@DiskJunky You should dive into the exception, there´s probably an inner exception telling you what´s whrong.
If you change the [XmlAttribute("Details") to [XmlElement("detail")] I'll mark this as the correct answer
1

This is just an additional background for what @Oscar mentions in his answer.

As often: favor composition over inheritance. Basically you should ask yourself if your Root-element is a list of Details, or if it has one. If in doubt consider the following:

Your Root-element should be an instance having some properties. It also has a list of elements within it, the Details. If you were after overriding the list, you say basically a Root is a list, however I want to override how that list works behind (for example you want to modify the Add-method to do something different). However In your case all you want to do is to extend the list instead of override it, don´t you?

1 Comment

agreed and that is indeed what I started with. It'd be my preference but frankly, I need something working over design principles :)
1
[XmlType("detail")]
public class Detail
{    
    [XmlElement("reference")]
    public string Reference { get; set; }}   
}
[XmlRoot("rootNode")]
public class Root
{
    [XmlAttribute("date")]
    public string Date { get; set; }

    [XmlAttribute("time")]
    public string Time { get; set; }

    [XmlElement("detail")]
    public Detail detail{ get; set; }
}

or you use "customized" serialization with IXmlSerializable Override XML Serialization Method

3 Comments

close but you forgot your closing } for the Detail object. Bumped up as very close to Oscar's solution
I'm not sure nesting the classes like that is the way to go. Apart from which,you have the detail object outside the root which is the inverse of what's needed structurally
fixed it again -> I still struggle with the stackoverflow "code editor" ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.