Dom-based XML file operations

Source: Internet
Author: User
InnerxmlReturns the XML fragment composed of all internal nodes of the xmlnode. Perform the following operations on elements that contain namespaces. InnertextReturns the internal text. <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

<XSL: Template>

...

</XSL: Template> </XSL: stylesheet>
<xsl:stylesheet version="1.0"       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="stock">         ...     </xsl:template>    </xsl:stylesheet>
The innerxml attribute on the style sheet node returns the following string:
<xsl:template  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     ...    </xsl:template>
<xsl:template match="stock"       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     ...    </xsl:template>
Assuming that the parent XSL: stylesheet element already has the xmlns: XSL namespace declaration, innerxml recognizes the xmlns: XSL namespace declaration as redundant and therefore removes it. If you move innerxml from a document without a default namespace to a document with a default namespace, the behavior is slightly different. Consider the following XML string:
<test>      <item>123</item>    </test>
<test>      <item>123</item>    </test>
Innerxml returns an unformatted XML string without the namespace declaration:
<item>123</item>
<item>123</item>
If this string is subsequently inserted into a document with a default namespace, for example, in the following situation:
<test2 xmlns="urn:1">    </test>
<test2 xmlns="urn:1">    </test>
Innerxml analyzes the string in the context. The new node selects the urn: 1 namespace. The result looks as follows:
<test2 xmlns="urn:1">      <item>123</item>    </test>
<test2 xmlns="urn:1">      <item>123</item>    </test>
When you request innerxml, you get the following results:
<item xmlns="urn:1">123</item>
<item xmlns="urn:1">123</item>
If you explicitly require that the inserted item retain the fact that it comes from a document without a namespace, You need to manually add the xmlns = "" declaration and insert the result string:
<item xmlns="">123</item>
<item xmlns="">123</item>
Xmlbeannet provides creation methods for all node types, including: Createcdatasection,Createcomment, createappsnetfragment, createdocumenttype, createelement, createnode, Createprocessinginstrucion, createsinglewhitespace, createtextnode, createwhitespace, and createxmldeclaration. After creating a node, you can add it to the tree in the following ways: Insertbefore, insertafter, appendchild, perpendchild, and append. The importnode of the xmldocument class can copy a node from an xmldocument object to another object without changing the original document. Then, it can be inserted into the target document in one way.The Demo code is as follows:

1 using system; 2 using system. XML; 3 4 namespace importxml 5 {6 class program 7 {8 public static void main (string [] ARGs) 9 {10 xmldocument doc1 = new xmldocument (); 11 doc1.loadxml ("<book> <title> Dream of Red Mansions </title> </book> "); 12 // create a new element node 13 xmlelement xem = doc1.createelement ("price"); 14 // create Attribute 15 xmlattribute att1 = doc1.createattribute ("format "); 16 // assign the attribute 17 att1.value = "RMB"; 18 // assign the attribute to the element's Node 19 xem. setattribu Tenode (att1); 20 // create text 21 xmltext text = doc1.createtextnode ("20"); 22 // Add text to the specified Element 23 xem. appendchild (text); 24 // Add the new element to the XML Object 25 doc1.documentelement. appendchild (xem); 26 xmldocument doc2 = new xmldocument (); 27 doc2.loadxml ("<author> CAO Xueqin </author> "); 28 // import the node from another document to the current document. 29 xmlnode newnode = doc1.importnode (doc2.documentelement, true); 30 doc1.documentelement. appendchild (newnode); 31 doc1.save (@ "D:/hlm1.xml"); 32 console. write ("press any key to continue"); 33 console. readkey (true); 34} 35} 36}

After the XML Document Object Model (DOM) is in the memory, you can remove the nodes in the tree or the content and value of a specific node type.Method for removing nodes in 1:

  • UseRemovechildMethod to remove a specific node. When the node is removed, the Child tree of the node will be removed.
  • UseRemoveallMethod to remove all sublevels and attributes of the current node.
How to remove an attribute from an attribute set in article 2: (use xmlattributecollection ATTS = xem. attributes; to obtain an attribute set of an element)
  • UseRemoveRemove a specific attribute.
  • UseRemoveallRemoves all attributes from the set and retains elements without any attributes.
  • UseRemoveatRemoves an attribute from a property set by using an index number.
3 Remove attributes from element nodes
  • UseRemoveallattributesRemove Attribute Set
  • UseRemoveattributeRemove a single attribute from the set by name.
  • UseRemoveattributeatRemoves a single attribute from the set by index.

Remove the attributes defined as default attributes in Document Type Definition (DTD). Unless the elements of the default attributes are removed, the default attributes are not removed.

When xmlattribute is called, The removeall method sets the attribute value to empty, because there cannot be no worthwhile attribute.

 

1 using system; 2 using system. XML; 3 4 namespace importxml 5 {6 class program 7 {8 public static void main (string [] ARGs) 9 {10 xmldocument doc1 = new xmldocument (); 11 doc1.loadxml ("<book> <title> Dream of Red Mansions </title> <price format = \" RMB \ "> 20 </price> <author> CAO Xueqin </author> </ book> "); 12 // obtain the root element 13 xmlelement root = doc1.documentelement; 14 // list of child nodes on the root element 15 xmlnodelist nodes = root. childnodes; 16 // obtain the second element 17 xmlelement e2 = nodes [1] As xmlelement; 18 // delete its attribute 19 e2.removeattribute ("format") with the name format "); 20 // obtain the subnode list of the second element 21 xmlnodelist NS = e2.childnodes; 22 // change the text value of the first node to 2523 NS [0]. value = "25"; 24 // Delete the first element of the XML fragment 25 root. removechild (root. firstchild); 26 doc1.save (@ "D:/hlm2.xml"); 27 28 console. readkey (true); 29} 30} 31}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.