Use DOM to read and write XML files

Source: Internet
Author: User

XML can transmit and store data between different applications on different platforms. 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. there are two models for accessing and operating XML files in an application: 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 documents 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 will 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 ("/Tables Table ");
For (int I = 0; I <nodes. Count; I ++)
{
// Obtain the total number of student information nodes
XmlNodeList nls = nodes [I]. ChildNodes;

// Cyclically fill fields of the student instance
For (int j = 0; j <nls. Count; j ++)
{
// Create a student instance
& Nbs

Related Article

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.