XML file operations, Basic Introduction

Source: Internet
Author: User

StatementArticleIn order to inspire others.

In a few words, go directly to the topic:

XML operations include obtaining node values, adding nodes, modifying nodes, and deleting nodes.

First, learn how to add nodes and add values to nodes!

Let's take a complete example.

 
1Filestream FS =NewFilestream (@"XML. xml", Filemode. Create );
2FS. Close ();

Remember using:

Using system. xml;
Using system. IO;

The above two rowsCodeIs to create a file named XML with the extension of XML, of course, to ensure thatProgramNo error. You can write filemode. openorcreate in filemode. This is written

During multiple running times, the original file is overwritten during new creation.

The classes required to operate XML documents are xmldocument, xmlelement, and xmlnode.

First, we need to use the xmldocument class to load XML documents:

Xmldocument XD = new xmldocument ();

We first use xmlelement to create two elements:

Xmlelement xet = XD. createelement ( "  People  " );
Xmlelement Xe = XD. createelement ( " More " );
Xe. setattribute ( " Name " , " Jack " );
Xe. setattribute ( " Age " , " 18 " );
Xe. setattribute ( " Gender " , " Man " );
- After writing the two nodes, we need to combine them in an orderly manner. The combined code is very simple: xet. appendchild (xe); XD. appendchild (xe); this means that the Xe element is used as a subnode of the xet element, and the xet is placed under the XD. Note: an XML document can only have one parent node, as shown below: < People > < More Name =" Jack " Age =" 18 " Gender =" Man " />  </People> The following XML document cannot be opened: < People >< More Name =" Jack " Age =" 18 " Gender =" Man " />

 </People> <People > < More Name =" Jack " Age =" 18 " Gender =" Man " />

 </People> After the operation, remember to save it. It is easy to save: XD. Save ("XML. xml ");

Open the XML document and you can see the following information:< People >   < More Name =" Jack " Age =" 18 " Gender =" Man "/>   </ People > This is the end of adding nodes. You can add n subnodes as needed. Next, let's get the node value. We also need to load the XML. XML document first:

 
1Xmldocument XD =NewXmldocument ();
2XD. Load ("XML. xml");
To obtain the value, first instantiate a Node object and then call the property directly to obtain the attribute value:

Xmldocument XD = new xmldocument ();
XD. Load (@ "XML. xml ");
 
Xmlnode xn = XD. selectsinglenode ("people ");
 
Xmlelement xet = XD. selectsinglenode ("More") As xmlelement;
 
Xet. getattribute ("Name");
 
XD. Save (@ "XML. xml ");
To modify a node, you also need to instantiate an xmlelement:

Xmldocument XD = new xmldocument ();
XD. Load (@ "XML. xml ");
 
Xmlnode xn = XD. selectsinglenode ("people ");
 
Xmlnode xn = XD. selectsinglenode ("People");
Xmlelement xet = XD. selectsinglenode ("More")AsXmlelement;
 
Xet. setattribute ("Name","Wills");
 
XD. Save (@ "XML. xml ");

 

The last step is to delete the node. If you delete the node, you can delete a node attribute and delete the entire node. First, delete a node attribute,

 
Xmldocument XD =NewXmldocument ();
XD. Load (@"XML. xml");
Xmlnode xn = XD. selectsinglenode ("People");
Xmlelement xet = XD. selectsinglenode ("More")AsXmlelement;
Xet. removeattribute ("Age");

Of course, you can also delete the node through the attribute index. The operation is similar to: xet. removeattributeat (0 );
Delete the entire node now!

Xmldocument XD =NewXmldocument ();
XD. Load (@"XML. xml");
Xmlnode xn = XD. selectsinglenode ("People");
Xmlelement xet = XD. selectsinglenode ("More")AsXmlelement;
Xet. removeall ();

In this case, the time is in a hurry. The more you write, the less you write. Sorry. For the first time I wrote a blog, I hope you can give me some support. I will explain it in detail in my later blog, and strive to get a quick start for new users.

Finally, I want to say that I am actually a tester.

 

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.