views:

56

answers:

2

Hi, I'm trying to deserialize an XML that comes from a web service but I do not know how to tell the serializer how to handlke this piece of xml:

<Movimientos>
<Movimientos>
<NOM_ASOC>pI22E7P30KWB9KeUnI+JlMRBr7biS0JOJKo1JLJCy2ucI7n3MTFWkY5DhHyoPrWs</NOM_ASOC>
<FEC1>RZq60KwjWAYPG269X4r9lRZrjbQo8eRqIOmE8qa5p/0=</FEC1>
<IDENT_CLIE>IYbofEiD+wOCJ+ujYTUxgsWJTnGfVU+jcQyhzgQralM=</IDENT_CLIE>
</Movimientos>
<Movimientos>

As you can see, the child tag uses the same tag as its parent, I suppose this is wrong however the webservice is provided by an external company and that wont change it, is there any way or any library to tidy up XML or how can I use an attribute on my class so that the serializer gets it right? thanks for any help.

+4  A: 

The serializer should be OK with it - the issue is probably just that a class can't have a property that's the same name as the class itself. So use XmlElementAttribute to match it up:

[XmlRoot("Movimientos")]
public class Movimientos
{
    [XmlElement("Movimientos")]
    public SomeOtherClass SomeOtherProperty { get; set; }
}

public class SomeOtherClass
{
    public string NOM_ASOC { get; set; }
    public string FEC1 { get; set; }
    public string IDENT_CLIE { get; set; }
}

FYI, the XML is actually not wrong; there's no rule in the spec saying that a child element can't have the same name as its parent element. It's unusual maybe, but still valid.

Aaronaught
I wouldn't say it's unusual or uncommon. If you define a menu structure, or other hierarchical lists (org chart) you'll have lots of child-parent elements of the same node name
Chad
@Chad: You generally shouldn't, at least not in .NET. The parent class would have a singular name and the child property name would be plural. It may only be a single-letter difference, but that's all it takes. If there's a nested 1:1 relationship then the convention is to use the prefix "Inner" or "Child". Not to suggest that every other way is wrong, but if you find yourself running into name collisions, then that's a good time to start thinking about a different naming convention.
Aaronaught
It's not necessarily a collision. In the case of a menu, every node is an object of the same type, and can recurse infinitely.
Chad
@Chad: I'm talking about a name collision in the class structure. That's what this question was about. All you have to do to avoid that is differentiate between singular and plural forms in the element names, which is semantically correct anyway.
Aaronaught
A: 

If this is not actually deserialized by the XML serializer of .Net, it's probably better to write your own class to parse it. You can use the System.Xml or System.Xml.Linq (easier) namespaces.

Jouke van der Maas