C # add, delete, modify, and query XML

Source: Internet
Author: User

XML is not used for a long time. Recently, in the Silverlight project, you need to access some C ++ Dll through Web Service.
Use XML to pass data. Let's just review the XML operations.

Meteorology Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Using System. Xml;
Using System. IO;

Namespace HCLoad. Web
{
Public partial class TestXml: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
LoadXml ();
}
Private void LoadXml ()
{
XmlDocument xmlDoc = new XmlDocument ();
String strXml =
@ "<? Xml version = "" 1.0 "" encoding = "" UTF-16 ""?>
<Projects>
<Project id = "1"> p1 </project>
<Project id = "2" ">
<Name> p2 </name>
</Project>
</Projects> ";
XmlDoc. LoadXml (strXml );

// Response. Write ("<script> alert (" + xmlDoc. OuterXml + "); </script>"); // OuterXml is all content contained by this node
// Response. Write (xmlDoc. OuterXml); // the xml document output directly in the browser is blank because the browser cannot parse these labels.

// Query by Attribute Value
XmlElement theProject = (XmlElement) xmlDoc. DocumentElement. SelectSingleNode ("/projects/project [@ id = 1]");
Response. Write (theProject. InnerText );

// Query by subnode Value
XmlElement theProject2 = (XmlElement) xmlDoc. DocumentElement. SelectSingleNode ("/projects/project [name = p2]");
Response. Write (theProject2.InnerText); // note that the layer of name is queried.

// Query by Tag Name and modify
XmlDoc. GetElementsByTagName ("project"). Item (0). InnerText =
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.