Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 0.64 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Changing the Format of XML
  2. <Root>
  3.   <Username>
  4.     <string>Fred</string>
  5.     <string>John</string>
  6.   </Username>
  7. </Root>
  8.        
  9. <Root>
  10.     <Username>Fred</Username>
  11.     <Username>John</Username>
  12.  </Root>
  13.        
  14. using System.Linq;
  15. using System.Xml.Linq;
  16.  
  17. var indoc = XDocument.Load("c:\test.xml");  
  18. var outdoc = new XDocument(
  19.               new XElement("Root",
  20.                 indoc.Descendants("Root")
  21.                      .Descendants("Username")
  22.                      .Elements()
  23.                      .Select(n => n.Value)
  24.                      .Select(i => new XElement("Username", i))));
  25.  
  26. // TODO: Save doc using doc.WriteTo(xmlWriter) to the file