Recall a C # addition, deletion, modification, and query of 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;

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

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.