Read and Write XML files

Source: Internet
Author: User

The XML file content is as follows: site. config

 

Code
<? XML version = " 1.0 " Encoding = " UTF-8 "   ?>
< Sites >
  < Site siteid = " 1 " Securitylevel = "" Publickey = "" Publicandprivatekey = "" Fromurlkey = "" Cryptkeyfield = "" Ivkeyfield = "" >
  </ Site >
  < Site siteid = " 2 " Securitylevel = "" Publickey = "" Publicandprivatekey = "" Fromurlkey = "" Cryptkeyfield = "" Ivkeyfield = "" >
  </ Site >
</ Sites >

 

Read XML

 

Code
String Export path = Path. Combine (appdomain. currentdomain. basedirectory, " Sites. config " );
Xmldocument Doc =   New Xmldocument ();
Doc. Load (upload path );
Xmlelement Root = Doc. documentelement; // : Get the root system. xml. xmlelement of the document.
Xmlnode Node = Root. selectsinglenode ( String . Format ( " Site [@ siteid = '{0}'] " , Siteid )); // Select the first xmlnode that matches the XPath expression.
Xmlattribute ATT = Node. attributes [ " Publickey " ];
If (ATT ! =   Null )
{
_ Publickey = Att. value;
}

 

Write XML

 

 

Code
String Export path = System. Io. Path. Combine (appdomain. currentdomain. basedirectory, " Sites. config " );
Xmldocument Doc =   New Xmldocument ();
Doc. Load (upload path );
Xmlelement Root = Doc. documentelement;
Xmlnode Node = Doc. createelement ( " Site " ); // Create an element with the specified name
Xmlattribute ATT = Doc. createattribute ( " Siteid " ); // Create attributes
Att. Value = _ Siteid;
Node. Attributes. append (ATT );

Root. appendchild (node );
Doc. Save (export path );

 

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.