1

My class definition:

[Serializable]
public class MyClass
{
    [XmlAttribute(AttributeName = "ID")] //Problem is here. same attr name ID.
    public int XXX_ID { get; set; }

    [XmlElement(ElementName = "XXX")]
    public string XXX_Value{ get; set; }

    [XmlAttribute(AttributeName = "ID")] //Problem is here. same attr name ID.
    public int YYY_ID { get; set; }

    [XmlElement(ElementName = "YYY")]
    public string YYY_Value { get; set; }
}

My XML:

<MyClass>
    <XXX ID="123">Some Values</XXX> 
    <YYY ID="567">Some Values</YYY>
</MyClass>

My Problem:

I want to de-serialize the above XML into an object.

During the runtime, an error has occurred, it is not allowed to have same attribute name in 2 different elements and under the same root.

How to solve this problem?

P/S: I cannot change the XML, I am not the owner of it.

Thanks in advance.

1
  • Note: [Serializable] does nothing useful here Commented Oct 31, 2011 at 11:03

1 Answer 1

2

To do that you either need to do the (de)serialization manually, or you need to DTO to have roughly the same layout as the xml. For example:

public class Something { // need a name here to represent what this is!
    [XmlAttribute] public int ID {get;set;}
    [XmlText] public string Value {get;set;}
}

then

public class MyClass {
    public Something XXX {get;set;}
    public Something YYY {get;set;}
}
Sign up to request clarification or add additional context in comments.

Comments

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.