C # xml operations (basic operations)

Source: Internet
Author: User

We usually see XML files like this.

 

<? XML version = "1.0"?>

{
Return false
} "Href =" http://writeblog.csdn.net/# "onfocus =" function onfocus ()
{
H ()
} "> -<Sqlconfig>   The SQL configuration information is saved here!  <Ip> 192.168.0.250</IP>  <Userid> SA</Userid>  <Password> SA</Password>  <Dbname> Hhfrm</Dbname>  <Bak_path> E:/backup</Bak_path>  </Sqlconfig>

When using XML, there are several basic requirements for literacy:

Xmldocument class: XML document, as its name implies. One of its objects represents an XML document. The above document is an xmldocument object.

Xmlelement class: an element in an XML document. For example, the IP address and userid are all elements.

Xmltext: XML text. For example, "the SQL configuration information is saved here" is a text.

Xmlnode class: indicates a node, which can be an element or a document.

 

How to create an XML document in the example:

// Initialize the XML document

Xmldocument Doc = new xmldocument ();

 

// Initialize the Element Node
Xmlelement xmlele_sqlconfig, xmlele_ip, xmlele_userid, xmlele_password, xmlele_dbname, xml_bak_path;

 

// Initialize the text node

Xmltext;

 

// Add the first element
Xmlele_sqlconfig = Doc. createelement ("sqlconfig"); // initialize the xmlele_sqlconfig Element
Xmltext = Doc. createtextnode ("the SQL configuration information is saved here! "); // Initialize a text node
Xmlele_sqlconfig.appendchild (xmltext); // Add the text node to the xmlele_sqlconfig element.
Doc. appendchild (xmlele_sqlconfig); // Add the xmlele_sqlconfig element to the XML text.

 

// Add a child element to the first element
Xmlele_userid = Doc. createelement ("userid"); // initialize the xmlele_userid element.
Xmltext = Doc. createtextnode (userid); // initialize the text node
Xmlele_userid.appendchild (xmltext); // Add the text node to the xmlele_userid element.
Xmlele_sqlconfig.appendchild (xmlele_userid); // Add the xmlele_userid element to the xmlele_sqlconfig element.

 

The other nodes will not be repeated .. There is also a way to add elements.

// Method 2 for adding a child element to the first element
Xml_bak_path = Doc. createelement ("bak_path"); // initialize the bak_path element.
Xml_bak_path.innertext = bak_path; // set text for the bak_path element. Instead, initialize the text first, and then add the text to the element.
Xmlele_sqlconfig.appendchild (xml_bak_path); // Add the xmlele_userid element to the xmlele_sqlconfig element.

 

 

Read:

 

// In the element sqlconfig, find the element named IP. And extract the value of its first child node (it may be the element or text, so it still needs innertext ).
String IP = sqlconfig. getelementsbytagname ("ip"). Item (0). innertext;

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.