1

I want to deserialze xml where I have 2 elements with the same tag but different values for their 'name' attributes. I want to do this without using an array.

The xml:

<rowset>
    <row name="row1" />
    <row name="row2" />
</rowset>

The class to deserialize into:

[XmlRootAttribute("rowset")]
public class Container
{
    [XmlElement("row")]
    public XmlNode row1;

    [XmlElement("row")]
    public XmlNode row2;
}

How can I target the right row element based on it's 'name' attribute's value?

That's if it's even possible?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Stuyvenstein
  • 2,340
  • 1
  • 27
  • 33
  • You need implement [IXmlSerializable](https://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable%28v=vs.110%29.aspx) and in `ReadXml()` have your custom logic. – abatishchev Feb 06 '15 at 21:21
  • If you want to give it a try http://stackoverflow.com/questions/13704752/deserialize-xml-to-object-using-dynamic – EZI Feb 06 '15 at 21:25
  • @EZI That is not really related to this question. Anyway, I have my own preferred methods of doing what those users wanted to achieve. – Stuyvenstein Feb 06 '15 at 21:37
  • 1
    @Anomaly I didn't say it is an answer and just posted an alternative. Why are you so aggressive? This area is for comments and they can be anything. If you don't want to use it then don't. You don't even to post a reply. – EZI Feb 06 '15 at 21:49
  • I'm not aggresive :) I just don't quite see how that question helps answer mine. Unless you are suggesting I implement my own structure and logic as @abatishchev too, is suggesting. It is something I want to try avoiding though, while hoping there is a way in the .NET framework to achive this easily. .NET has always proven itself to be very powerful and exstensive IMO so I'm confident it has some way I can do this without changing much of my existing method. I'll continue digging in the MSDN articles in the meantime – Stuyvenstein Feb 06 '15 at 22:00

0 Answers0