c# - How to populate xml from list of paths -


how populate xml list of paths in c#?

for example:

c:\windows\addins c:\windows\apppatch c:\windows\apppatch\mui c:\windows\apppatch\mui\040c c:\windows\microsoft.net\framework\v2.0.50727 c:\windows\microsoft.net\framework\v2.0.50727\mui c:\windows\microsoft.net\framework\v2.0.50727\mui\0409 

as input list, output should be:

<node label="c:">    <node label="windows">       <node label="apppatch">          <node label="mui">             <node label="040c" />          </node>       </node>       <node label="microsoft.net">          <node label="framework">             <node label="v2.0.50727">                <node label="mui">                   <node label="0409" />                </node>             </node>          </node>       </node>       <node label="addins" />    </node> </node> 

can me this, i'm trying more week without result?

this sounds suspiciously homework, hey, it's future. ;)

var paths = new string[]          {             "c:\\windows\\addins",             "c:\\windows\\apppatch",             "c:\\windows\\apppatch\\mui",             "c:\\windows\\apppatch\\mui\\040c",             "c:\\windows\\microsoft.net\\framework\\v2.0.50727",             "c:\\windows\\microsoft.net\\framework\\v2.0.50727\\mui",             "c:\\windows\\microsoft.net\\framework\\v2.0.50727\\mui\\0409"         };          const string node = "node";         const string label = "label";          var xml = new xelement("nodes");          foreach (var path in paths)         {             var labelvalues = path.split('\\');             var currentnode = xml;              foreach (var labelvalue in labelvalues)             {                 var foundnode = currentnode.elements(node).where(n => (string)n.attribute(label) == labelvalue).singleordefault();                 if (foundnode != null)                 {                     currentnode = foundnode;                 }                 else                 {                     var newnode = new xelement(node, new xattribute(label, labelvalue));                     currentnode.add(newnode);                     currentnode = newnode;                 }             }         } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -