ASP. NET operation XML reading and saving

Source: Internet
Author: User
XML Code

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Env>
<Param name = "rate" value = "3"/>
<Param name = "url" value = "3"/>
</ENV>
<VR startpano = "start" width = "10" Height = "540" x = "0" Y = "0">
<Pano name = "url" url = "car/model/interiorpanorama/bjxd_i30/S-max.jpg">
</Pano>
</VR>
<Background file = "control/bg.jpg">
</Background>
</Root> backend CS code Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml;

Public partial class somepages_operatexml: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = readxml ();
Lblreadxml. Text = STR;
Savexml ();
}
Private string readxml ()
{
String strreturn = string. empty;

Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("~ /Images/config. xml "));
Xmlelement root = Doc. documentelement;

// Define the matching item name = "url"
String Path = @ "// * [@ name = 'url']";
// Search for matching items on the current node
Foreach (xmlnode node in root. selectnodes (PATH ))
{
Strreturn + = "Match:" + path;
Strreturn + = "node:" + node. Name;
Strreturn + = "<br/> ";
}

// Directly search for the read value of the specified Node
Xmlelement x = (xmlelement) root. selectsinglenode ("Vr ");
If (X! = NULL)
{
Strreturn + = x. getattribute ("startpano ");
}
Return strreturn;

}
Private void savexml ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("~ /Images/config. xml "));

Xmlelement root = Doc. documentelement;
Xmlelement x = (xmlelement) root. selectsinglenode ("Vr ");

If (X! = NULL)
{
// Set node attributes
X. setattribute ("width1", "10 ");
// Create a node
Xmlelement X1 = Doc. createelement ("test ");
X1.setattribute ("width", "100 ");
// Add a node
X. appendchild (X1 );
// Save the XML
Doc. Save (server. mappath ("~ /Images/config. xml "));
}
}
}

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.