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;
UsingSystem. xml;
UsingSystem. 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 = " P11 " ;
// Response. Write ("<SCRIPT> alert ('" + xmldoc. outerxml + "'); </SCRIPT> ");
// Add attribute
Xmlelement theelement = Xmldoc. documentelement. firstchild As Xmlelement;
Theelement. setattribute ( " No " , " 001 " );
// Response. Write ("<SCRIPT> alert ('" + xmldoc. outerxml + "'); </SCRIPT> ");
//Delete a node
//Theelement. parentnode. removechild (theproject );
// Delete a node set
Xmlnodelist nodelist = Xmldoc. documentelement. selectnodes ( " /Projects/project [@ ID <3] " );
For ( Int I = 0 ; I < Nodelist. Count; I ++ )
{
Nodelist. Item (I). parentnode. removechild (nodelist. Item (I ));
}
Response. Write ( " <SCRIPT> alert (' " + Xmldoc. outerxml + " '); </SCRIPT> " );
}
}
}
See a newbie's note:
Http://www.cnblogs.com/huazaizai/archive/2010/07/01/1769095.html