0

I have a dynamic XML structure which is looks like this:

<RES>
<A>value 1</A>
<B>value 2</B>    
<C>value 3</C>
</RES>

My question is how can I parse it to Dictionary<string, string> object?

I tried this but it is no working:

[XmlElement(ElementName = "RES")]
public Dictionary<string, string> result { get; set; }

Could you please help me on it?

Zaosz
  • 137
  • 1
  • 2
  • 11
  • 2
    Does this answer your question? [Deserialize XML into Dictionary](https://stackoverflow.com/questions/9210059/deserialize-xml-into-dictionarystring-string) – GSerg Jun 24 '21 at 13:28
  • I read that article but maybe I haven't got too much experience in this level. So I have to implement IXmlSerializable interface and use it instead of XmlElement ? – Zaosz Jun 24 '21 at 14:00
  • Or use linq to xml as shown. – GSerg Jun 24 '21 at 14:32
  • 1
    I have choose this way to implement what I need: https://gist.github.com/refractalize/1751260 It is working fine for me. Thank you all of you for your help. – Zaosz Jun 28 '21 at 09:08
  • @Zaosz - that gist looks quite clean and simple. Do you want to make an answer yourself? If not this could be closed as a duplicate of [Deserialize XML into Dictionary](https://stackoverflow.com/q/9210059/3744182) (for loading with Linq-to-XML then using `ToDictionary()` plus [Deserialize XML with XmlSerializer where XmlElement names differ but have same content](https://stackoverflow.com/q/45766597/3744182) (for the `[XmlAnyElement] public XElement[] XElements { ... }` surrogate property trick). – dbc Jun 28 '21 at 17:24
  • Thank you! it is working fine. – Zaosz Apr 22 '22 at 17:48

0 Answers0