Working with XML documents in C #

Source: Internet
Author: User

First, we'll start by building an XML document, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<peoplelist>
<person>
<name>Tom Stafford</name>
<title>CFO</title>
</person>
<person>
<name>Jane Goodwill</name>
<title>CEO</title>
</person>
<person>
<name>Tim Daly</name>
<title>CTO</title>
<title2>CTO0</title2>
</person>
<person>
<name>John Graver</name>
<title>CSO</title>
</person>
</peoplelist>

Save it as: People.xml,:)

(1) Find the specified node in the XML document

For example, to find the <title2> of a user named ' Tim Daly ', the specific procedure is as follows.

----

<%@page language="C#"%>
<%@import namespace="System.Xml"%>
<%@import namespace="System.Xml.Xsl"%>
<%@import namespace="System.Xml.XPath"%>
<script language="c#" runat="server">
void page_load(Object obj,EventArgs e)
{
string xmlfile="people.xml",xpath;
xpath=Server.MapPath(xmlfile);
XmlDocument myDoc = new XmlDocument(); //定义一个XmlDocument对象。
myDoc.Load(xpath);
message.Text=myDoc.SelectSingleNode ("//person[name='Tim Daly']").ChildNodes.Item(2).InnerText;}
</script>
<asp:label id="message" forecolor="red" runat="server"/>

Explain:

where//Represents the child nodes of any layer inside. This will soon find what you want.

selectSingleNode is to find a single node, selectnodes can find many nodes

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.