Read data from XML connection parameters-XML read operation

Source: Internet
Author: User

When I used Delphi programming, the data connection information was generally stored in INIFILE, And the password was encrypted. C # programming, I store the connection parameters in the XML file and encrypt the password. Multiple connections can be stored in an XML file.

The XML file for storing data source connection parameters is as follows:

<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Connections>
<Connection name = "kkerp" DESC = "Open Group ERP Database" type = "SQL">
<Param name = "datasource" caption = "Data Source" value = "(local)"/>
<Param name = "userid" caption = "User ID" value = "sa"/>
<Param name = "databasename" caption = "Database" value = "kkerp"/>
<Param name = "integratedsecurity" caption = "Integrated Security" value = "sspi"/>
</Connection>
<Connection name = "petshop" DESC = "Pet Store Data" type = "Ole">
<Param name = "provider" caption = "provider" value = "Microsoft. Jet. oledb.4.0"/>
<Param name = "datasource" caption = "Data Source" value = "petshop. mdb"/>
</Connection>
</Connections>

The following code reads the data connection string of the specified data connection name:

/** // <Summary>
/// Read the connection string
/// </Summary>
/// <Param name = "connname"> connection name </param>
/// <Returns> </returns>
Private string readconnstring (string connname)
...{
// Read the data connection string
String strconn = "";
String filename = @ "conn. xml ";
Filename = application. startuppath + "/" + filename;
String strxml;
Streamreader reader = new streamreader (filename );
Strxml = reader. readtoend ();
Xmldocument xmldoc = new xmldocument ();
Xmldoc. loadxml (strxml );
// Read all connections
Xmlnodelist nodelist = xmldoc. selectsinglenode ("Connections"). childnodes;
// Query the parameter set of the connection Template
For (INT I = 0; I <nodelist. Count; I ++)
...{
// Whether the connection is
Xmlelement Xe = (xmlelement) nodelist [I];
If (Xe. getattribute ("name") = connname)
...{
// Connection Parameters
Foreach (xmlnode node in Xe. childnodes)
...{
Xmlelement xe1 = (xmlelement) node;
Strconn + = xe1.getattribute ("caption") + "=" + xe1.getattribute ("value ");
}
Break;
}
}
Reader. Close ();
//
Return strconn;
}

We first read the text content of the XML file from conn. xml and store it in strxml using (streamreader) reader. Use xmldocument xmldoc to load strxml text.

In this case, you can use xmldoc to read the node name, node value, and attribute value. The connection parameter name and value are read and combined into a string.

If you have a password, use encryption/decryption.

 

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.