Use Dom to read and write XML files

Source: Internet
Author: User

XML can be used on different platformsProgramData transmission and storage. it is an information transmission tool independent of software and hardware. therefore, it has become the W3C recommendation standard. currently, software and hardware configuration files are generally stored in XML files. XML files are easy to use and can be accessed in any application. access in applicationsThere are two models for operating XML files: Dom (Document Object Model) and stream model. The advantage of Dom is that it allows you to edit and update XML documents, you can randomly access the data in the document and use XPath for query. However, the disadvantage of Dom is that it needs to load the entire document to the memory at a time. For large documents, this may cause resource problems. let's not talk about this much. Let's look at a simple example of using Dom to create and read and write XML files.

Xmlprocess. CS:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. xml;

Namespace xmlread
{
Public class xmlprocess
{
// Create an XML Object
Xmldocument xmldoc;

// Define a Node object
Xmlnode node;

// Obtain the current working directory path
String workdir = directory. getcurrentdirectory ();

List <student> stulist = new list <student> ();

// Define an element
Xmlelement xmlele;

Public void creatnewxml ()
{
// Create an XML file in the working directory
// Instantiate an XML Object
Xmldoc = new xmldocument ();

// Add the Declaration section of the XML file, <? XML version = "1.0" encoding = "gb2312"?>
Xmldeclaration xmldecl;
Xmldecl = xmldoc. createxmldeclaration ("1.0", "gb2312", null );
Xmldoc. appendchild (xmldecl );

// Add a root element
Xmlele = xmldoc. createelement ("", "External table ","");
Xmldoc. appendchild (xmlele );
// Obtain the root node
Xmlnode root = xmldoc. selectsinglenode ("orders table ");

// Add a student record
Xmlelement stu1 = xmldoc. createelement ("Student 1 ");

Xmlelement stuno = xmldoc. createelement ("student ID ");
Stuno. innertext = "1001 ";
Xmlelement stuname = xmldoc. createelement ("name ");
Stuname. innertext = "James ";
Xmlelement stugrade = xmldoc. createelement ("score ");
Stugrade. innertext = "96 ";
Stu1.appendchild (stuno );
Stu1.appendchild (stuname );
Stu1.appendchild (stugrade );
Root. appendchild (stu1 );

// Add the second student record
Xmlelement stu2 = xmldoc. createelement ("Student 2 ");

stuno = xmldoc. createelement ("student ID");
stuno. innertext = "1002";
stuname = xmldoc. createelement ("name");
stuname. innertext = "Li Si";
stugrade = xmldoc. createelement ("score");
stugrade. innertext = "90";
stu2.appendchild (stuno);
stu2.appendchild (stuname);
stu2.appendchild (stugrade);
root. appendchild (stu2);

// The preceding method is used to manually add an XML file. It is used to create and write an XML file.
// You can also use the xmltextwriter object to output data streams.

// Generate and save the XML file
Xmldoc. Save (workdir + "\ studentgrade. xml ");
}

Public void dataIn (string filename)
{
Xmldoc = new xmldocument ();

// load the XML file
xmldoc. load (workdir + "\" + filename);
// obtain the root node
xmlnodelist nodes = xmldoc. selectnodes ("/orders table");
for (INT I = 0; I {< br> // total number of nodes for obtaining Student Information
xmlnodelist NLS = nodes [I]. childnodes;

// Cyclically fill fields of the student instance
For (Int J = 0; j <NLS. Count; j ++)
{
// Create a student instance
Student Stu = new student ();

Node = NLS [J]. selectsinglenode ("student ID ");
Stu. studentnum = int. parse (node. innertext );

Node = NLS [J]. selectsinglenode ("name ");
Stu. Name = node. innertext;

Node = NLS [J]. selectsinglenode ("score ");
Stu. Grade = int. parse (node. innertext );
Stulist. Add (Stu );
}
}
}

Public void dataout ()
{< br> // output Student Information
for (INT I = 0; I {< br> Student Stu = stulist [I];
console. writeline (Stu. studentnum. tostring () + "" + Stu. name + "" + Stu. grade. tostring ();
}< BR >}

Static void main (string [] ARGs)
{
Xmlprocess xmlpro = new xmlprocess ();
Xmlpro. creatnewxml ();
Xmlpro. dataIn ("studentgrade. xml ");
Xmlpro. dataout ();
Console. readkey (true );
}

}
}

Student. CS:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace xmlread
{
Public class student
{
Private int studentnum;
Private string name;
Private int grade;
Public int studentnum {get {return studentnum;} set {studentnum = value ;}}
Public string name {get {return name;} set {name = value ;}}
Public int grade {get {return grade;} set {grade = value ;}}
}
}

If you place main () in a separate class file and call the methods of the other two classes in this class file, system. stackoverflowexception will be triggered.

When too many nested method calls cause execution stack overflow, The stackoverflowexception exception is thrown, so the main () is placed in the XML operation class.

PartCodeReference: http://www.cnblogs.com/gb2013/archive/2009/10/04/1577855.html

 

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.