C # XML programming for the warm and newest Learning Series-XmlDocument class (1)

Source: Internet
Author: User

  Preface

. NET Framework XML class provides an XML analyzer object XmlDocument, which is the core object for most XML operations. When operating XML documents, you must first define an XmlDocument object, load the XML file into the memory, and then read and write the XML file.

  Reading directory

1: Write an XML file

2: code file writing

  Instance

How can we get the name, gender, and age we want?

  Steps

  1: Write an XML file

XMLFile. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Students>
<Student>
<Name> Zhang San </name>
<Sex> male </sex>
<Age> 27 </age>
</Student>
<Student>
<Name> Luxi </name>
<Sex> female </sex>
<Age> 26 </age>
</Student>
</Students>

  2: code file writing

Form1.cs

Using System. Data;

Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Xml;

Namespace XmlDocumentClass
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{

// Declare an XmlDocument object
XmlDocument xml_doc = new XmlDocument ();

// Load the compiled XMLFile. xml file to the memory. Here you can use the relative address or absolute address. Here we use the relative address.
Xml_doc.Load ("XMLFile. xml ");

// The DocumentElement attribute indicates that the root node for obtaining the XML document will obtain the selected node SelectSingleNode ("/Students ") the method is to obtain the first XmlNode that matches the XPath expression query, that is, it will continue to obtain all the nodes under Students under the node I selected in the figure, and finally get the node I selected

XmlNode xml_node = xml_doc. DocumentElement. SelectSingleNode ("/Students ");

Figure 1

      

Figure 2
// Obtain information about Michael Jacob.
MessageBox. Show (xml_node.ChildNodes [0]. InnerText );

But if we only want the name "Zhang San", we can do this.

// Get a node set, that is, get the node information in Figure 3

XmlNodeList xml_node_list = xml_doc.DocumentElement.SelectNodes ("student ");

      

Figure 3

// Obtain the text information of the first child node

String strName = xml_node_list [0]. ChildNodes [0]. InnerText;
MessageBox. Show (strName );

      
}
}
}

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.