C # XML Read-write instance

Source: Internet
Author: User
Tags xpath

First, the use of System.Xml

Example: Complete the Read and write operation of the following format configuration file:

<?xml version="1.0" encoding="UTF-8"? ><! DOCTYPE doc[]><root>  <DatabaseInfo>    <HostName>127.0.  0.1</HostName>    <DatabaseName>orcl</DatabaseName>    <username>scott</ username>    <Password>scott</Password>  </DatabaseInfo></root>

Implementation code:

Read operation public static string Getconectstringxml () {XmlDocument xmldoc = new XmlDocument (); Xmldoc.load (Dbconfigfilepath);            Load XML Document String XPath = "/root";            XmlNode XmlNode = Xmldoc.selectsinglenode (XPath);                if (XmlNode! = null) {XmlNode = Xmlnode.selectsinglenode ("/root/databaseinfo");                var TST = Xmlnode.selectsinglenode ("/root/databaseinfo/hostname"); String hostName = TST.                INNERXML; String sid = Xmlnode.selectsinglenode ("/root/databaseinfo/databasename").                INNERXML; String uid = Xmlnode.selectsinglenode ("/root/databaseinfo/username").                INNERXML; string pwd = Xmlnode.selectsinglenode ("/root/databaseinfo/password").                INNERXML;                StringBuilder connectstr = new StringBuilder ();                Connectstr.append ("Data source=" (Description= (address_list= (address=) (PROTOCOL=TCP); Connectstr.append (HostName);                Connectstr.append (") (port=1521))) (Connect_data= (service_name=");                Connectstr.append (SID); Connectstr.append (")));                User id= ");                Connectstr.append (UID); Connectstr.append (";                Password = ");                Connectstr.append (PWD); Connectstr.append (";                Unicode=true ");            connectionString = Connectstr.tostring ();        } return connectionString; }//write operation public static void Setconectstringxml (String hostname,string sid,string uid,string pwd) {XMLDOCU            ment xmldoc = new XmlDocument (); Xmldoc.load (Dbconfigfilepath);            Load XML Document String XPath = "/root";            XmlNode XmlNode = Xmldoc.selectsinglenode (XPath);                if (XmlNode! = null) {XmlNode = Xmlnode.selectsinglenode ("/root/databaseinfo");                var subnode = Xmlnode.selectsinglenode ("/root/databaseinfo/hostname"); Subnode.innerxml = HostName;                subnode = Xmlnode.selectsinglenode ("/root/databaseinfo/databasename");                Subnode.innerxml = SID;                subnode = Xmlnode.selectsinglenode ("/root/databaseinfo/username");                Subnode.innerxml = UID;                subnode = Xmlnode.selectsinglenode ("/root/databaseinfo/password");                Subnode.innerxml = pwd;            Xmldoc.save (Dbconfigfilepath); }        }

  

C # XML Read-write instance

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.