This article describes how to process XML documents in C.
First, we first build an XML document, as shown below:
<? 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 search for a user named 'Tim daly' <title2>, 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 (); // defines an xmldocument object.
Mydoc. Load (XPath );
Message. Text = mydoc. selectsinglenode ("// person [name = 'Tim daly']"). childnodes. Item (2). innertext ;}
</SCRIPT>
<Asp: Label id = "message" forecolor = "red" runat = "server"/>
Explanation:
// Represents any layer of sub-nodes. In this way, you can quickly find what you want.
Selectsinglenode is used to find a single node. selectnodes can find many nodes.
(2) Fill the list box with XML documents. Only the <Name> node is used here.
<% @ Page Language = "C #" DEBUG = "true" %>
<% @ Import namespace = "system. Collections" %>
<% @ Import namespace = "system. xml" %>
<HTML>
<Head>
<Script language = "C #" runat = "server">
Private void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Arraylist values = new arraylist (); // use it as a data source
Xmldocument Doc = new xmldocument (); // represents an XML document
Doc. Load (server. mappath ("People. xml "));
// Returns an xmlnodelist set that contains a list of all child elements matching the specified name.
Xmlnodelist elemlist = Doc. getelementsbytagname ("name"); // system. xml namespace
For (INT I = 0; I <elemlist. Count; I ++)
{
Values. Add (elemlist [I]. innerxml );
}
Listbox1.datasource = values;
Listbox1.databind ();
}
}
// Obtain the text of the list item and selected item
Private void submitbtn_click (Object sender, eventargs E)
{
If (listbox1.selectedindex>-1)
Label1.text = "selected option:" + listbox1.selecteditem. Text + "<p> ";
}
</SCRIPT>
</Head>
<Body>
<Form runat = Server>
<H3> Data Binding ListBox <Asp: Label id = "label1" font-name = "verdana" font-size = "10pt" runat = "server"/>
<Asp: ListBox id = "listbox1" selectionmode = "single" rows = "1" runat = "server"/>
<Asp: button id = "button1" text = "Submit" onclick = "submitbtn_click" runat = "server"/>
</Form>
</Body>
</Html>
========================================================== ======
(3) read XML documents into repeater through dataset.
You can also read it into dataset. The principle is the same.
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Data" %>
<% @ Page Language = "C #" %>
<HTML>
<Head>
<Title> Read to repeater </title>
<Script language = "C #" runat = Server>
Public void page_load (Object OBJ, eventargs E)
{
String XPath = "DB/people. xml ";
Try
{
Dataset DS = new dataset ();
Filestream FS = new filestream (server. mappath (XPath), filemode. Open, fileaccess. Read, fileshare. readwrite );
DS. readxml (New streamreader (FS ));
FS. Close ();
Trace. Warn ("Number of table records", convert. tostring (Ds. Tables [0]. Rows. Count ));
Mydatalist. datasource = Ds. Tables [0]. defaultview;
Mydatalist. databind ();
}
Catch (exception ed)
{
Response. Write ("<font color = # ff0000>" + ed. tostring () + "</font> ");
}
}
</SCRIPT>
</Head>
<Body>
<Asp: repeater id = "mydatalist" runat = "server">
<Headertemplate>
<H5> viewer details </H5>
</Headertemplate>
<Itemtemplate>
<Br>
<Table class = "mainheads" width = "60%" style = "Font: 8pt verdana">
<Tr style = "background-color: # ffffcc">
<TD> name: </TD>
<TD> <% # databinder. eval (container. dataitem, "name") %> </TD>
</Tr>
<Tr style = "background-color: # ffffcc">
<TD> title: </TD>
<TD> <% # databinder. eval (container. dataitem, "title") %> </TD>
</Tr>
<Tr style = "background-color: # ffffcc">
<TD> title2: </TD>
<TD> <% # databinder. eval (container. dataitem, "title2") %> </TD>
</Tr>
</Table> <br>
</Itemtemplate>
</ASP: repeater>
</Body>
</Html>
---------
(4) Save the data in repeater to XML. Similarly, you can also use the data in dataset.
In this example, a new row is added and the edited result is saved to XML
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Data" %>
<% @ Page Language = "C #" trace = "true" %>
<HTML>
<Head>
<Title> Saurabh's XML counter script </title>
<Script language = "C #" runat = Server>
Public void page_load (Object OBJ, eventargs E)
{
String datafile = "DB/people. xml ";
If (! Page. ispostback)
{
Try {
Dataset DS = new dataset ();
Filestream fint;
Fint = new filestream (server. mappath (datafile), filemode. Open, fileaccess. Read, fileshare. readwrite );
DS. readxml (fint );
Fint. Close ();
If (session ["counter"] = NULL)
{
Datarow DR = Ds. Tables [0]. newrow ();
Dr ["name"] = "myname ";
Dr ["title"] = "test ";
Dr ["title2"] = "Test2 ";
DS. Tables [0]. Rows. Add (DR );
Filestream fout;
Fout = new filestream (server. mappath (datafile), filemode. Open, fileaccess. Write, fileshare. readwrite );
DS. writexml (fout, xmlwritemode. writeschema );
Fout. Close ();
Session ["counter"] = "set ";
}
Trace. Warn ("Number of table records", DS. Tables [0]. Rows. Count. tostring ());
}
Catch (exception EDD)
{
Response. Write ("<font color = # ff0000>" + EDD. tostring () + "</font> ");
}
}
}
</SCRIPT>
</Body>
</Html>
----------------------
(5) read XML data in the URL
C # the XML data in the URL is read. Normally, static XML files are read, but XML data is updated at any time in the securities field.
For example, if a third party wants us to provide XML data, we will develop it for the second party, and the third party will provide us with a URL, which is in XML format and contains the required data.
The example here is to read the URL on csdn.
Using system;
Using system. xml;
Namespace consoleapplication5
{
Class class1
{
[Stathread]
Static void main (string [] ARGs)
{
Xmldocument Doc = new xmldocument ();
Doc. Load ("http://www.csdn.net/expert/topic/1094/1094085.xml? Temp =. 9642145 ");
Doc. Save (console. Out );
}
}
}
In this way, the C # program will display the XML uploaded from the remote ASP page in console. Out (console output.