【C# XML】 System.Xml类


命名空间的整体结构

 

新建XmlDocument()方法

方法一:new  XmlDocument();new  XmlDocument(XmlImplementation imp);new  XmlDocument(XmlNameTable);

方法二:XmlImplementation实例创建一个有共同上下文的xml文档,内部有一个XmlNameTable

       XmlImplementation xmlImplementation = new XmlImplementation();
       XmlDocument doc1= xmlImplementation.CreateDocument();
       XmlDocument doc2 = xmlImplementation.CreateDocument();//doc2和doc1有共同的XmlNameTable ,方便比较两个文档。
//相当于new XmlDocument(XmlImplementation imp) ;

XmlDocument 新建一个文档

  XmlDocument xmldoc = new XmlDocument();
        XmlImplementation impl = new XmlImplementation();
        XmlDocument doc2 = impl.CreateDocument();
        XmlDeclaration declaration =doc2.CreateXmlDeclaration("1.0",null,null);
     


        //添加一个新节点
        XmlElement root = doc2.CreateElement("Books");
        XmlElement decendant = doc2.CreateElement("Book");
        XmlElement decendant2 = doc2.CreateElement("Title");

        //添加属性
        root.SetAttribute("name", "root");

        //修改属性
        decendant2.InnerText = "zhanyuhepin";

        //添加克容节点
        decendant.AppendChild(decendant2);
        root.AppendChild(decendant);
        doc2.AppendChild(root);

        //添加xml 声明
        doc2.InsertBefore(declaration, root);
        doc2.Save("books.xml");
    

NameTable\XmlNameTable


XmlReaderSettings 、XmlNamespaceManager、XmlDocument有NameTable,XmlImplementation内部有XmlNameTable。
有些类(如 XmlDocument 和 XmlReader)在内部使用 NameTable 类存储属性名和元素名。 当 XML 文档中多次出现某个元素名或特性名时,该名称在 NameTable 中只存储一次。
例如:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"Books.xml");
//Books.xml包含以下内容:
"http://example.books.com"> "autobiography" publicationdate="1991" ISBN="1-861003-11-0">

xmldoc种的nametable就会存储 bookstore、sp、http://example.books.com、genre 。

管理命名空间:XmlNamespaceManager

XmlNamespaceManager类存储命名空间 URI 及其前缀的集合,您可在此集合中查找、添加和删除命名空间。 在某些上下文中,需要使用此类以获得更佳 XML 处理性能。 例如,XsltContext 类、 XmlDocument.SelectNodes方法使用 XmlNamespaceManager 以获得 XPath 支持。XmlDocument使用XmlNamespaceManager管理命名空间。lingXDocument不使用XmlNamespaceManager管理命名空间。

命名空间管理器对命名空间不执行任何验证,而是假定前缀和命名空间已经过验证并符合 W3C 命名空间规范。

下面是可使用 XmlNamespaceManager 类执行的一些管理和查找任务。 有关更多信息和示例,请遵循指向每个方法或属性的引用页的链接。

功能使用
添加命名空间 AddNamespace 方法
移除命名空间 RemoveNamespace 方法
查找默认命名空间的 URI DefaultNamespace 属性
查找命名空间前缀的 URI LookupNamespace 方法
查找命名空间 URI 的前缀 LookupPrefix 方法
获取当前节点中命名空间的列表 GetNamespacesInScope 方法需要XmlNamespaceScope枚举做参数
限定命名空间的范围 PushScope 和 PopScope 方法
检查是否在当前范围内定义了前缀 HasNamespace 方法
获取用于查找前缀和 URI 的名称表 NameTable 属性

应用实例

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(@"Books.xml");
        XmlNameTable nameTable = xmldoc.NameTable;
        XmlNamespaceManager xmlns = new XmlNamespaceManager(xmldoc.NameTable);
   
        xmlns.AddNamespace("sd", "http://example.books.com");//默认的命名空间也要添加 前缀
        xmlns.AddNamespace("sp",   "http://example.book.com");
        XmlNodeList nodelist = xmldoc.SelectNodes("/sd:bookstore/sp:book", xmlns);

        XmlNodeList nodelist = xmldoc.SelectNodes("//*[local-name()='bookstore']");  这将选择名称为 Section 的所有元素,而不管它们的命名空间。
//*[local-name()='Section' and namespace-uri()='http://www.siemens.com/automation/Openness/SW/Interface/v1'] 很多处理xpath的工具也有注册命名空间的方法,然后就可以使用像这样的限定形式


local-name() 是 xpath函数

Books.xml

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://example.books.com">
  <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklintitle>
    <author>
      <first-name>Benjaminfirst-name>
      <last-name>Franklinlast-name>
    author>
    <price>8.99price>
  book>
  <sp:book genre="novel" publicationdate="1967" ISBN="0-201-63361-2" xmlns:sp="http://example.book.com">
    <title>The Confidence Mantitle>

    <author>
      <first-name>Hermanfirst-name>
      <last-name>Melvillelast-name>
    author>
    <price>11.99price>
  sp:book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgiastitle>
    <author>
      <name>Platoname>
    author>
    <price>9.99price>
  book>
bookstore>
 

读取一个xml片段

string xmlFrag ="hammer " +
                        "paint" +
                        "saw";

// Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("rk", "urn:store-items");

// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

// Create the reader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), settings, context);
C