.NET 中 XML 操作与 XPath、XSLT 的应用
1. XML 文档创建基础
在 .NET 里创建 XML 文档时,首先要构建声明部分和根元素。以下是示例代码:
//create the declaration section
XmlDeclaration newDec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(newDec);
//create the new root element
XmlElement newRoot = doc.CreateElement("newBookstore");
doc.AppendChild(newRoot);
在这个示例中, CreateXmlDeclaration
方法用于创建 XML 声明,其参数分别是版本(目前通常为 1.0)、编码和独立标志。若编码参数为 null,则默认使用 UTF - 8;独立标志可以是 “yes”、“no” 或 null,若为 null,该属性不会包含在文档中。接着创建的元素会成为文档元素,这里命名为 newBookstore
。运行上述代码生成的 booksEdit.xml
如下:
<?xml version="1.0"?>
<newBookstore>
<book genre="Mystery" publicationdate="200