xml操作总结
2017-06-04 17:29 Dirichlet 阅读(260) 评论(0) 编辑 收藏 举报1. Document有两个子节点,xml头描述节点和book节点。book节点是top level点节点,可以通过DocumentElement属性得到。是能有一个top level element节点。Only one top level element is allowed in an XML document.
<?xml version="1.0" encoding="gb2312"?> <book genre="novel" ISBN="1-861001-57-5"> <Appearance> <ColumnHeaderCell> <Interior>Solid; 181, 206, 251</Interior> <Font> <Bold>true</Bold> </Font> </ColumnHeaderCell> </Appearance> <title>Pride And Prejudice</title> <price>19.95</price> <Appearance> <ColumnHeaderCell> <Interior>Solid; 181, 206, 251</Interior> <Font> <Bold>true</Bold> </Font> </ColumnHeaderCell> </Appearance> </book>
Element是Node的子集,XmlNode表示一个节点,包括XmlElement(元素)和XmlAttribute(属性)等。 如:
<Alarm lock="true"> //node
<Time> //node
StringValue //node
</Time> //node
</Alarm> //node
以上Alarm(元素节点),lock(属性节点),Time(元素节点),StringValue(文本节点)都是Node,但是只有 <Alarm>......</Alarm>和<Time>StringValue</Time>是Element
XmlNode: 属性节点()、注释节点、文本节点(XmlText)、元素节点(XmlElement)等。
System.Xml.XmlDeclaration 表示 XML 声明节点:<?xml version='1.0'...?>。
// Summary: // Specifies the type of node. public enum XmlNodeType { // Summary: // This is returned by the System.Xml.XmlReader if a Read method has not been // called. None = 0, // // Summary: // An element (for example, <item> ). Element = 1, // // Summary: // An attribute (for example, id='123' ). Attribute = 2, // // Summary: // The text content of a node. Text = 3, // // Summary: // A CDATA section (for example, <![CDATA[my escaped text]]> ). CDATA = 4, // // Summary: // A reference to an entity (for example, # ). EntityReference = 5, // // Summary: // An entity declaration (for example, <!ENTITY...> ). Entity = 6, // // Summary: // A processing instruction (for example, <?pi test?> ). ProcessingInstruction = 7, // // Summary: // A comment (for example, <!-- my comment --> ). Comment = 8, // // Summary: // A document object that, as the root of the document tree, provides access // to the entire XML document. Document = 9, // // Summary: // The document type declaration, indicated by the following tag (for example, // <!DOCTYPE...> ). DocumentType = 10, // // Summary: // A document fragment. DocumentFragment = 11, // // Summary: // A notation in the document type declaration (for example, <!NOTATION...> // ). Notation = 12, // // Summary: // White space between markup. Whitespace = 13, // // Summary: // White space between markup in a mixed content model or white space within // the xml:space="preserve" scope. SignificantWhitespace = 14, // // Summary: // An end element tag (for example, </item> ). EndElement = 15, // // Summary: // Returned when XmlReader gets to the end of the entity replacement as a result // of a call to System.Xml.XmlReader.ResolveEntity(). EndEntity = 16, // // Summary: // The XML declaration (for example, <?xml version='1.0'?> ). XmlDeclaration = 17, }
插入一段xml格式文本到原xml。
1. 创建XmlDocument,载入xml片段,导入原XmlDocument得到XmlNode,最后插入这个节点。
static void Testcase3() { XmlDocument doc0 = new XmlDocument(); doc0.Load("data.xml"); XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml("<Appearance><ColumnHeaderCell><Interior>Solid; 181, 206, 251</Interior><Font><Bold>true</Bold></Font></ColumnHeaderCell></Appearance>"); XmlNode nodeInsert2 = doc.ImportNode(doc2.DocumentElement, true); XmlDocument doc3 = new XmlDocument(); doc3.LoadXml("<Appearance><ColumnHeaderCell><Interior>Solid; 181, 206, 251</Interior><Font><Bold>true</Bold></Font></ColumnHeaderCell></Appearance>"); XmlNode nodeInsert3 = doc.ImportNode(doc2.DocumentElement, true); XmlNode root = doc.DocumentElement; //Create a new node. XmlElement elem = doc.CreateElement("price"); elem.InnerText = "19.95"; //Add the node to the document. root.InsertAfter(elem, root.FirstChild); root.AppendChild(nodeInsert2); root.InsertBefore(nodeInsert3, root.FirstChild); Console.WriteLine("Display the modified XML..."); doc.Save(Console.Out); Console.WriteLine(); }
2. 使用XmlDocumentFragment,省去了ImportNode的过程。
static void StepOne(string fileName) { // original xml document XmlDocument docMain = new XmlDocument(); docMain.Load(fileName); // Create the XmlDocument. XmlDocumentFragment docFrag = docMain.CreateDocumentFragment(); docFrag.InnerXml = "<Appearance><ColumnHeaderCell><Interior>Solid; 181, 206, 251</Interior><Font><Bold>true</Bold></Font></ColumnHeaderCell></Appearance>"; XmlDocumentFragment docFrag2 = docMain.CreateDocumentFragment(); docFrag2.InnerXml = "<Appearance><ColumnHeaderCell><Interior>Solid; 181, 206, 251</Interior><Font><Bold>true</Bold></Font></ColumnHeaderCell></Appearance>"; // Create a new element and add it to the document. XmlNode nodeGridGridEngine = docMain["Grid"]["GridSchema"]["GridEngine"]; XmlNode nodeTableDescriptor = nodeGridGridEngine["TableDescriptor"]; nodeGridGridEngine.InsertAfter(docFrag, nodeTableDescriptor); //GridGroupingLookAndFeel, TableOptions, XmlNode nodeGridGroupingLookAndFeel = docMain["Grid"]["GridGroupingLookAndFeel"]; XmlNode nodeTableOptions = nodeGridGroupingLookAndFeel["TableOptions"]; nodeGridGroupingLookAndFeel.InsertAfter(docFrag2, nodeTableOptions); docMain.Save(fileName); }