C # some basic operations on XML files

Source: Internet
Author: User

 

Code
// Define xmldocument
Xmldocument =   New Xmldocument ();
// Define XML document header files
Xmldeclaration = Xmldocument. createxmldeclaration ( " 1.0 " , " UTF-8 " , Null );
// Add XML Document Header
Xmldocument. appendchild (xmldeclaration );
// Define the root of XML
Xmlelement xmlroot = Xmldocument. createelement ( " Userdata " );
// Add root attributes
Xmlroot. setattribute ( " Createuser " , " True " );
// Modify the root Attribute Value
Xmlroot. getattributenode ( " Createuser " ). Value =   " False " ;
// Add a subnode and set its attributes
Xmldocument. appendchild (xmlroot );
Xmlelement dataconnection
= Xmldocument. createelement ( " Dataconnection " );
Xmlelement Server
= Xmldocument. createelement ( " Server " );
Server. innertext
=   " Localhost " ;
Xmlelement uid
= Xmldocument. createelement ( " UID " );
UID. innertext
=   " SA " ;
Xmlelement pwd
= Xmldocument. createelement ( " PWD " );
Xmlroot. appendchild (dataconnection );
Dataconnection. appendchild (server );
Dataconnection. appendchild (UID );
Dataconnection. appendchild (PWD );
// Save XML document
Xmldocument. Save ( " Book. xml " );
// Reads the server value of a subnode.
Xmlnode xnserver = Xmldocument. selectsinglenode ( " Userdata/dataconnection/Server " );
Console. writeline (
" Node server's value is " + Xnserver. innertext );
Console. Readline ();

Generated XMLCodeAs follows:

 

Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Userdata Createuser = "False" >
< Dataconnection >
< Server > Localhost </ Server >
< UID > SA </ UID >
< PWD />
</ Dataconnection >
</ Userdata >

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.