Some examples of reading, modifying, and deleting XML files in ASP. NET

Source: Internet
Author: User

There are many ways to read xml in. net. I hope the tutorial will help you to organize several common xml file modifications and read and delete instances.

ASP. NET can read XML files in four ways for analysis. For more information, see.
Method 1: Use the XML Control

The Code is as follows:

The Code is as follows: Copy code

<% @ Page Language = "C #" %>
<Html>
<Body>
<H3> <font face = "Verdana"> XML method 1 </font> <From runat = server>
<Asp: Xml id = "xml1" DocumentSource = "grade. xml" runat = "server"/>
</From>
</Body>
</Html>

Method 2: Use DOM technology

The Code is as follows:

The Code is as follows: Copy code
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Xml" %>
<% @ Import Namespace = "System. Xml. Xsl" %>
<Html>
<Script language = "C #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument ();
Doc. Load (Server. MapPath ("grade. xml "));
Xml1.Document = doc;
}
</Script>
<Body>
<H3> <font face = "Verdana"> method 2 of XML reading </font> <From runat = server>
<Asp: Xml id = "xml1" runat = "server"/>
</From>
</Body>
</Html>

Method 3: Use a DataSet object
The Code is as follows:

The Code is as follows: Copy code
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<Script language = "C #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
DataSet objDataSet = new DataSet ();
ObjDataSet. ReadXml (Server. MapPath ("grade. xml "));
DgEmployees. DataSource = objDataSet. Tables ["student"]. DefaultView;
DgEmployees. DataBind ();
}
</Script>
<Body>
<H3> <font face = "Verdana"> XML reading method 3 </font> <Asp: DataGrid id = "dgEmployees" runat = "server"/>
</Body>
</Html>

Method 4: Read data by text

The Code is as follows:

The Code is as follows: Copy code
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Xml" %>
<Html>
<Script language = "C #" runat = "server">
Private void Page_Load (Object sender, EventArgs e)
{
XmlTextReader objXMLReader = new XmlTextReader (Server. MapPath ("grade. xml "));
String strNodeResult = "";
XmlNodeType objNodeType;
While (objXMLReader. Read ())
{
ObjNodeType = objXMLReader. NodeType;
Swith (objNodeType)
{
Case XmlNodeType. XmlDeclaration:
// Read the XML File Header
StrNodeResult + = "XML Declaration: <B>" + objXMLReader. Name + "" + objXMLReader. Value + "</B> <br/> ";
Break;
Case XmlNodeType. Element:
// Read tags
StrNodeResult + = "Element: <B>" + objXMLReader. Name + "</B> <br/> ";
Break;
Case XmlNodeType. Text:
// Read value
StrNodeResult + = "-Value: <B>" + objXMLReader. Value + "</B> <br/> ";
Break;
}
// Determine whether the node has attributes
If (objXMLReader. AttributeCount> 0)
{// Judge all nodes cyclically
While (objXMLReader. movetonextatti.pdf)
{// Obtain the tag and Value
StrNodeResult + = "-Attribute: <B>" + objXMLReader. name + "</B> value: <B>" + objXMLReader. value + "</B> <br/> ";
}
}
LblFile. Text = strNodeResult;
}
}
</Script>
<Body>
<H3> <font face = "Verdana"> XML reading method 4 </font> <From runat = server>
<Asp: label id = "LblFile" runat = "server"/>
</From>
</Body>
</Html>

Add a deleted instance


The XML file name is bcastr. xml.

The structure is as follows:

The Code is as follows: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Bcaster>
<Item id = "79" item_url = "PicNews/Img/u = 404630538,2075277077" link = "HTML/050/AI_20081017_50_53_79.html" itemtitle = "111111111111111111"/>
<Item id = "78" item_url = "PicNews/Img/Index_04_01_06.jpg" link = "HTML/050/AI_20081017_50_53_78.html" itemtitle = "zengjia"/>
<Item id = "77" item_url = "PicNews/Img/bsxwf.jpg" link = "HTML/050/AI_20081017_50_53_77.html" itemtitle = "Graduate Department of China Pharmaceutical University"/>
<Item id = "76" item_url = "PicNews/Img/Jiangning Damen .jpg" link = "HTML/050/AI_20081017_50_53_76.html" itemtitle = ""/>
<Item id = "75" item_url = "PicNews/Img/Chinese Pharmaceutical University certification (perfect 22.16.jpg" link = "HTML/050/AI_20081017_50_53_75.html" itemtitle = "News test picture news"/>
</Bcaster>

Add node functions:

///

The Code is as follows: Copy code

Write the image news information to the XML file set of the image news player.
/// </Summary>
/// <Param name = "picpath"> image path </param>
/// <Param name = "htmlpath"> image news URL </param>
/// <Param name = "title"> title </param>
Public void WritePicNewsXML (string picpath, string htmlpath, string title, string aid)
{
XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
// If (xnl. Count <= 5) // keep no more than 5 homepage pictures and news
//{
XmlNode rootnode = xmlDoc. SelectSingleNode ("bcaster ");

XmlElement fel = (XmlElement) rootnode. FirstChild;

XmlElement el = xmlDoc. CreateElement ("item"); // Add subnodes and attributes
El. SetAttribute ("id", aid );
El. SetAttribute ("item_url", picpath );
El. SetAttribute ("link", htmlpath );
El. SetAttribute ("itemtitle", title );
Rootnode. PrependChild (el); // Add the newly added image news to the first position.
If (xnl. Count> 5)
{
XmlNode lxn = rootnode. LastChild;
Rootnode. RemoveChild (lxn); // deletes the final picture news.
}


XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
//}
}
Function for modifying the attributes of an XML node:

/// <Summary>
/// Modify XML attributes
/// </Summary>
/// <Param name = "picpath"> </param>
/// <Param name = "htmlpath"> </param>
/// <Param name = "title"> </param>
/// <Param name = "aid"> </param>
Public void EditPicNewsXML (string picpath, string htmlpath, string title, string aid)
{

XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. GetAttribute ("id") = aid) // rewrite if a node exists
{
Xe. SetAttribute ("item_url", picpath );
Xe. SetAttribute ("link", htmlpath );
Xe. SetAttribute ("itemtitle", title );
Break;
}
}

XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
}

Function used to delete a specified XML node:

The Code is as follows: Copy code

/// <Summary>
/// Delete the specified XML Node
/// </Summary>
/// <Param name = "aid"> </param>
Public void DelPicNewsXML (string aid)
{
XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. GetAttribute ("id") = aid) // delete a node if it exists
{
Xe. RemoveAll ();
Break;
}
}

XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
}

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.