c# - read XML, save it into dictionary with multiple values -


ok have following xml:

<root>     <item id="1" level="1" />     <item id="2" level="1">         <item id="3" level="2"/>         <item id="4" level="2">             <item id="5" level="3">                 <item id="6" level="4" />             </item>         </item>         <item id="7" level="2" />     </item> </root> 

i dictionary output this, way can insert data sql database,

id | parentid  | level ------------------------ 1     null         1 2     null         1 3      2           2 4      2           2 5      4           3 6      5           4 7      2           2 

currrently code first 2 columns, not sure how 3rd column "level" show in dictionary.

xelement root = xelement.parse(strserializedoutput); dictionary<int, int> list = root.descendants("item").todictionary(x => (int)x.attribute("id"), x =>     {         var parentid = x.parent.attribute("id");         if (parentid == null)             return 0;         return (int)parentid;     }); 

really, use use datatable, buuut potentially use anonymous type well:

xelement root = xelement.parse(strserializedoutput); var list = root.descendants("item")     .todictionary(x => (int)x.attribute("id"), x =>     {         var parentid = (int)x.parent.attribute("id");         var level = (int)x.parent.attribute("level");          return new { parentid = parentid, level = level };     }); 

so example, when write db, you'd use list.parentid , list.level.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -