fork(25) download
  1. using System;
  2. using System.Xml;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. var xml = @"<?xml version=""1.0""?>
  9. <listOfItems>
  10. <Item id=""1"">
  11. <attribute1 type=""foo""/>
  12. <attribute2 type=""bar""/>
  13. <property type=""x""/>
  14. <property type=""y""/>
  15. <attribute3 type=""z""/>
  16. </Item>
  17. <Item id=""2"">
  18. <attribute1 type=""aaa""/>
  19. <attribute2 type=""bbb""/>
  20. <property type=""ccc""/>
  21. <property type=""ddd""/>
  22. <attribute3 type=""ee""/>
  23. </Item>
  24. </listOfItems>";
  25.  
  26. var xmldoc = new XmlDocument();
  27. xmldoc.LoadXml(xml);
  28. XmlNodeList nodes = xmldoc.GetElementsByTagName("Item");
  29. foreach(XmlNode node in nodes)
  30. {
  31. Console.WriteLine("Node={0} type id={1}", node.Name, node.Attributes["id"].Value);
  32. foreach(XmlNode child in node.SelectNodes("property"))
  33. {
  34. Console.WriteLine(" Name={0} type attribute={1}", child.Name, child.Attributes["type"].Value);
  35. }
  36. }
  37. }
  38. }
Success #stdin #stdout 0.11s 25752KB
stdin
Standard input is empty
stdout
Node=Item type id=1
    Name=property type attribute=x
    Name=property type attribute=y
Node=Item type id=2
    Name=property type attribute=ccc
    Name=property type attribute=ddd