0

I have a document with this kind of structure:

<page name="some-name">
    <header>
        //some content
    </header>
    <section header="value">
        //some content
    </section>
</page>

When I deserialize this document to get an instance of Page class, I always get reflection error and InvalidOperationException. I figured out by debugging and a lot of try and error that the reason for this error is that i have the same name for a node (XMLElement) and a property (XMLAttribute), in this example the name is "header". XML structure is not changeable in any way, so that is not the solution. Is there a way to make it work, or do I have to add the property value later, outside of deserialization?

Classes are in this form:

[XmlType("page")]
public class Page
{
    [XmlAttribute("name")]
    public string Name { get; set }

    [XmlElement("header")]
    public Header Header { get; set }

    [XmlElement("section")]
    public Section Section { get; set }
}

[XmlType("section")]
public class Section
{
    [XmlAttribute("header")]
    public string Header { get; set }
}

[XmlType("header")]
public class Header
{
    //elements and attributes as properties
}
Milos Maksimovic
  • 299
  • 2
  • 5
  • 17

2 Answers2

1

use [XmlRoot("page")] to specify the name of generated xml root element instead of using [XmlType("page")]

https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute(v=vs.110).aspx

u7pro
  • 36
  • 2
0

You can try to generate classes from XML using Xsd.exe and see how it manages to solve this. Here is more info about classes generation from XML.

Community
  • 1
  • 1
Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55