C # XML Summary 1

Source: Internet
Author: User
C # operation XML Summary 1 many articles on the Internet that use C # To operate XML, but all of them work together to complicate the simple problem and leave it blank for reference. If you copy the hard cover, it will not only fail to reach the goal, but will waste time. I have previously done XML operations, but I forgot to do so. So I checked it online this afternoon. It really makes me sad. The solution on the internet is complicated and not very useful, and it makes me very shameless. I decided to sort it out for your reference tonight.
XmlDocument and XmlNode are mainly used. XML is enough for these two classes. We recommend that you do not use the XmlElement class because the XmlElement class requires you to define the XML document structure, this is completely unnecessary for general simple applications, and cannot be used without definition.
Take a simple example:
XML document content
<? Xml version = "1.0" encoding = "UTF-8"?>
<SDEConfig>
<SDEServer> APPL_GW_SERVER </SDEServer>
<SDEUser> RelicImg </SDEUser>
<SDEPassWord> gw2008 </SDEPassWord>
<SDEDatabase> GW </SDEDatabase>
</SDEConfig>

C # Read the source code of XML

Using System. Xml;

# Region link SDE information Definition variable
// Server
Private string m_SDEServer = "";
// User
Private string m_SDEUser = "";
// Password
Private string m_SDEPassWord = "";
// Database
Private string m_SDEDatabase = "";

# Endregion

/// <Summary>
/// Initialize SDE configuration information
/// </Summary>
Private void InitSDEConfiguration ()
{
String outPut = string. Empty;
Try
{
String xmlpath = Application. StartupPath + "\ sdeconfig. xml ";
If (! System. IO. File. Exists (xmlpath ))
{
MessageBox. Show ("the SDE configuration file is missing! "," Reading SDE configuration information ", MessageBoxButtons. OK, MessageBoxIcon. Warning );
Return;
}
XmlDocument doc = new XmlDocument ();
Doc. Load (xmlpath );

XmlNode node = doc. SelectSingleNode ("SDEConfig/SDEServer ");
This. m_SDEServer = node. InnerText;
// User
This. m_SDEUser = doc. SelectSingleNode ("SDEConfig/SDEUser"). InnerText;
// Password
This. m_SDEPassWord = doc. SelectSingleNode ("SDEConfig/SDEPassWord"). InnerText;
// Database
This. m_SDEDatabase = doc. SelectSingleNode ("SDEConfig/SDEDatabase"). InnerText;
}
Catch (Exception ex)
{
MessageBox. Show ("failed to read SDE configuration information! "," Reading SDE configuration information ", MessageBoxButtons. OK, MessageBoxIcon. Warning );
}
}

If you want to readProperty Information, <SDEServer Name = "wm"> APPL_GW_SERVER </SDEServer>
Yes
XmlNode xmlnode = doc. SelectSingleNode ("SDEConfig/SDEServer ");
// Read the attribute value
String strAttributeName = xmlnode. Attributes ["Name"]. Value. ToString ();

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.