Linq To XML 中使用到的以X开头的类都在 System.Xml.Linq 命名空间下。如下图所示:
XObject
|-XAttribute
|-XNode
|-XComment
|-XContainer
| |-XElement
| |-XDocument
|-XDocumentType
|-XProcessingInstruction
|-XText
|-XCData
另外比较有用的类:XStreamElement 用于处理大数据量的XML,以提高性能。
1. XML文档作成:
利用 XDocument, XElement, XAttribute, XDeclaration 作成XML。
将会输出下面的结果:
2. 通过 XNamespace 指定命名空间
Console.WriteLine(e.Name);
XNamespace ns1 = "http://ns.handcraft.org/testnamespace";
XElement e1 = new XElement(ns1 + "product",new XAttribute(XNamespace.Xmlns + "e1", ns), new XAttribute("id", "RX78"),
new XElement(ns1 + "name", "Gundam"), new XElement("append", "nonamespace"));
Console.WriteLine("先头追加Namespace:");
Console.WriteLine(e1);
}
将会输出以下结果:
3. XPathEvaluate, XPathSelectElements (这些是扩展方法,命名空间:System.Xml.XPath)
通过 XPathEvaluate, XPathSelectElements 使用XPath功能。
下面是示例的XML:
XPathEvalute的使用示例:
结果如下:
Programing Something
Administrating Something
XPathSelectElements的使用示例:
结果如下: