Asp.net reads xml, and asp.net reads xml

Source: Internet
Author: User

Asp.net reads xml, and asp.net reads xml

This is suitable for students who have just learned asp.net. the great gods just skipped it. asp.net often has a lot of places where XML is used, such as the Association of provinces and cities nationwide, and some menu reads all have xml shadows, directly paste the code so that I can forget it when I use it later. I have clearly written the comments and can leave a message if I don't understand it.

/// <Summary> /// read the city list /// </summary> public void CityList () {StringBuilder sb = new StringBuilder (); xmlDocument xmldoc = new XmlDocument (); xmldoc. load (Server. mapPath ("/XmlData/City. xml "); // load xml XmlNodeList xmlnlist = xmldoc. selectNodes ("/Cities/City"); // read the xml node list for (int I = 0; I <xmlnlist. count; I ++) {sb. append ("<a href = \" index. aspx? Cityid = "+ xmlnlist [I]. attributes ["ID"]. value + "\"> "+ xmlnlist [I]. attributes ["CityName"]. value + "</a>"); // read the Value in the loop} citys = sb. toString ();} // ------ attached xml format ------ // <? Xml version = "1.0" encoding = "UTF-8"?> <Cities> <City ID = "218" CityName = "Nanning" PID = "20" ZipCode = "530000"> Nanning </City> <City ID = "219" CityName = "Liuzhou" PID = "20" ZipCode = "545000"> Liuzhou </City> <City ID = "228" CityName = "Hezhou" PID = "20" ZipCode = "542800 "> Hezhou </City> </Cities>

In addition, I have added the QQ logon function at www.weixh.net over the past few days. I am still studying it and will share the process with you after the review!


How to Use aspnet C # To Read xml

DataSet ds = new DataSet ();
If it is a file:
Ds. ReadXml (file path );

If it is a string:
XmlDocument xd = new XmlDocument ();
Xd. LoadXml (string );
XmlNodeReader xnr = new XmlNodeReader (xd );
Ds. ReadXml (xnr );

This will fill in the DataSet ..

Complete code
/// <Summary>
/// XML Resource Type
/// </Summary>
Public enum XmlType
{
File,
String
};
/// <Summary>
/// Read the XML resource to the able
/// </Summary>
/// <Param name = "source"> XML resource. The file is a path; otherwise, it is an XML string. </param>
/// <Param name = "xmlType"> XML Resource Type: file, string </param>
/// <Param name = "tableName"> table name </param>
/// <Returns> DataTable </returns>
Public DataTable GetTable (string source, XmlType xmlType, string tableName)
{
DataSet ds = new DataSet ();
If (xmlType = XmlType. File)
{
Ds. ReadXml (source );
}
Else
{
XmlDocument xd = new XmlDocument ();
Xd. LoadXml (source );
XmlNodeReader xnr = new XmlNodeReader (xd );
Ds. ReadXml (xnr );
}

Return ds. Tables [tableName];
}... Remaining full text>

How does aspnet read xml strings?

XmlDocument xmlDoc = new XmlDocument ();

If (! File. Exists (xmlFileName ))
{
Return string. Empty;
}
XmlDoc. Load (xmlFileName );
XmlNodeList nodes = xmlDoc. SelectNodes (keyPath );

Foreach (XmlNode node in nodes)
{
Foreach (XmlNode childNode in node. ChildNodes)
{
If (childNode. Name = keyName. ToUpper ())
{
String dataValue = childNode. InnerText;
Return dataValue;
}
}
}

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.