c# - Converting XML To dictionary -


i converting 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> 

into dictionary using this:

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

where pair is:

    public class pair     {         int parentid;         int level;     } 

output want be:

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

but getting error saying

error 35 cannot implicitly convert type 'system.collections.generic.dictionary int,anonymoustype#1' 'system.collections.generic.dictionary int,proposalsystem.handlers.main.pair'

xelement root = xelement.parse(strserializedoutput); dictionary<int, pair> list = root.descendants("item")                                  .todictionary(x => (int) x.attribute("id"),                                   x => {                             var pid = x.parent.attribute("id");                             var depthlevel = x.attribute("level");                             return pid == null ? new pair { parentid = 0, level = (int)depthlevel } :                             new pair { parentid = (int)pid, level = (int)depthlevel };                           });  public class pair {     public int parentid;     public int 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 -