Basic operations on XML files in. net

Source: Internet
Author: User

I used some XML knowledge when I made a program last week. Today I will make a small summary of this knowledge point. If there are any shortcomings, I hope you will forgive me!

First, the most important thing about XML files is to allow users to define their own nodes, subnodes, and attributes, which makes it easier to access information resources, the custom specifications mentioned above are all passed. the schema file of XSD is used as a constraint. As for how to write this file, I will not talk about it for the moment, because I have never written it. The following describes how to use a program in. Net to read and create an XML file.

1. Read and create

Xmldocument is a class that must be instantiated to operate XML files. Read the existing XML files on the disk:

Create an instance of the xmldocument class: xmldocument Doc = new xmldocument ();

Call the load () method to load an existing XML file on the disk. The parameter is the complete path of the XML file: Doc. Load ("yourxmlfilepath ");

The following describes how to create an XML file using a program: the XML file must be instantiated and the xmldocument class should be created as well:

Create an instance of the xmldocument class: xmldocument Doc = new xmldocument ();

The xmldocument class has many methods to create the content and attributes of XML nodes. Of course, the nodes can contain subnodes. The following is an example of creating a simple XML file:

Xmldocument Doc = new xmldocument ();
Xmlelement chartarea = Doc. createelement ("chartarea"); // create an XML element, which can include child nodes and attributes.
Xmlelement series = Doc. createelement ("series ");
Series. setattribute ("name", "Series1"); // set attributes for the node
Series. setattribute ("ID", "001 ");
Doc. appendchild (chartarea); // Add the node to the end of the XML document. Here it is the first node.
Chartarea. appendchild (series); // Insert the second node to the first node
Xmlelement datapoints = Doc. createelement ("datapoints"); // Layer 3 Node
Series. appendchild (datapoints );
Xmlelement datapoint = Doc. createelement ("datapoint"); // Layer 4 node
Datapoint. setattribute ("X", "0 ");
Xmlelement y = Doc. createelement ("Y ");
Y. innertext = "10 ";
Datapoints. appendchild (datapoint );
Datapoint. appendchild (y); // Load

The content of the XML document obtained by this program is as follows:

<Chartarea>
<Series name = "Series1" id = "001">
<Datapoints>
<Datapoint x = "0">
<Y> 10 </Y>
</Datapoint>
</Datapoints>
</Series>
</Chartarea>

How are you doing? Easy! As you can see here, we should ask, how is this result output? The answer is as follows:

Xmltextwriter writer = new xmltextwriter (console. Out); // output to the console
Writer. Formatting = formatting. indented; // output format, indented with the data child element
Doc. writecontentto (writer); // output the document to the stream
Writer. Flush (); // refresh the basic stream
Doc. Save ("C: // y. xml") // save it as a physical file

2. Insert data from a specified Node
Xmlnode xmltmp = Doc. selectsinglenode ("chartmap/chartareas/chartarea/serieses"); // retrieve a Single Node
For (INT I = 0; I <5; I ++) // Add five datapoint elements under the obtained Node
{
Xmlelement point1 = Doc. createelement ("datapoint ");
Xmlattribute xvalue = Doc. createattribute ("xvalue ");
Xvalue. innertext = J. tostring ();
Point1.setattributenode (xvalue );
Xmlelement yvalue = Doc. createelement ("yvalue ");
Yvalue. innertext = "20 ";
Point1.appendchild (yvalue );
Pointsnode. appendchild (point1 );
}

Multiple nodes: xmlnodelist nodes = Doc. selectnodes (string XPath );

3. Add the node and content from another XML object to the specified node of another XML object:

Using system. xml;

Public class sample
{
Public static void main ()
{
Xmldocument Doc = new xmldocument ();
Doc. loadxml ("<bookstore>" +
"<Book genre = 'noel' ISBN = '1-861001-57-5 '>" +
"<Title> Pride and Prejudice </title>" +
"</Book>" +
"</Bookstore>"); loads XML documents with strings

// Read another XML file

Xmldocument doc2 = new xmldocument ();
Doc2.load ("books. xml ");

// Assign the value of the content of the specified node in the doc to a newbook.
Xmlnode newbook = Doc. importnode (doc2.documentelement. lastchild, true );
Doc. documentelement. appendchild (newbook); add newbook to the root node of Doc.

Doc. Save (console. Out );

.

}
}
Now, I only know so much about XML file operations. Let's talk about this! In some cases, you still need to look at msdn.

 

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.