xml解析之利器--XDocument,XElement,XAttribute等类

  1. //XElement
  2. //一个根节点中包含多个子节点,一个节点相当一个XElement
  3.             XElement contacts =
  4.                 new XElement("Contacts",
  5.                     new XElement("Contact",
  6.                         new XElement("Name""Patrick Hines"),
  7.                         new XElement("Phone""206-555-0144"),
  8.                         new XElement("Address",
  9.                             new XElement("Street1""123 Main St"),
  10.                             new XElement("City""Mercer Island"),
  11.                             new XElement("State""WA"),
  12.                             new XElement("Postal""68042")
  13.                         )
  14.                     )
  15.                 );
  16.             Console.WriteLine(contacts);
  17.             //XDocument xdoc = new XDocument();
  18.             //xdoc.Add(contacts);
  19.             //xdoc.Save(@"d:/test.xml");
  20.             //d:/test.xml
  21.             //<?xml version="1.0" encoding="utf-8"?>
  22.             //<Contacts>
  23.             //  <Contact>
  24.             //    <Name>Patrick Hines</Name>
  25.             //    <Phone>206-555-0144</Phone>
  26.             //    <Address>
  27.             //      <Street1>123 Main St</Street1>
  28.             //      <City>Mercer Island</City>
  29.             //      <State>WA</State>
  30.             //      <Postal>68042</Postal>
  31.             //    </Address>
  32.             //  </Contact>
  33.             //</Contacts>
  34.             //XElement,from,where,select
  35.             XElement srcTree = new XElement("Root",
  36.                 new XElement("Element", 1),
  37.                 new XElement("Element", 2),
  38.                 new XElement("Element", 3),
  39.                 new XElement("Element", 4),
  40.                 new XElement("Element", 5)
  41.             );
  42.             XElement xmlTree = new XElement("Root",
  43.                 new XElement("Child", 1),
  44.                 new XElement("Child", 2),
  45.                 from el in srcTree.Elements()
  46.                 where (int)el > 2
  47.                 select el
  48.             );
  49.             Console.WriteLine(xmlTree);
  50.             XDocument xdoc = new XDocument();
  51.             xdoc.Add(xmlTree);
  52.             xdoc.Save(@"d:/test2.xml");
  53.             //d:/test2.xml
  54.             //<?xml version="1.0" encoding="utf-8"?>
  55.             //<Root>
  56.             //  <Child>1</Child>
  57.             //  <Child>2</Child>
  58.             //  <Element>3</Element>
  59.             //  <Element>4</Element>
  60.             //  <Element>5</Element>
  61.             //</Root>
  62. //XAttribute
  63.             XDocument xDoc=new XDocument(new XDeclaration("1.0""UTF-8""yes"),
  64.                 new XComment("test the XDocoument"),
  65.                 new XElement("Persion",
  66.                     new XAttribute("name""sky"),
  67.                     new XAttribute("age", 25),
  68.                     new XElement("company",
  69.                         new XAttribute("name","IBM")
  70.                         )
  71.                     )
  72.                 );
  73.             Console.WriteLine(xDoc);
  1. //报错:“找不到where关键字”
  2. //解决方法:
  3. //1.在参照设定中追加System.Core
  4. //2.使用命名空间:using System.Linq;

你可能感兴趣的:(xml,IBM,encoding)