I have a string containing field/value pairs in xml format and want to parse it into Dictionary object.
string param = "<fieldvaluepairs>
<fieldvaluepair><field>name</field><value>books</value></fieldvaluepair>
<fieldvaluepair><field>value</field><value>101 Ways to Love</value></fieldvaluepair>
<fieldvaluepair><field>type</field><value>System.String</value></fieldvaluepair>
</fieldvaluepairs>";
Is there a quick and simple way to read and store all the field/value pairs in Dictionary object? No LINQ please.
Also, there could be possibility that the value field can contain an xml string itself. the solutions provided is wonderful but fails if the value is xml iteself. Example: If the value is something like
<?xml version="1.0" encoding="utf-8"?><GetCashFlow xmlns="MyServices"><inputparam><ac_unq_id>123123110</ac_unq_id></inputparam></GetCashFlow>
It errors out with the message:
Unexpected XML declaration. The XML
declaration must be the first node in
the document, and no white space
characters are allowed to appear
before it.
Please feel free to suggest any modification in xml format if you think it can make things easier to store in Dictionary object.
1 Answer