Reading and updating XML files in Asp.net

Source: Internet
Author: User

1. Example XML file model. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Drroot>
<Sitename> XML operation example </sitename>
<Siteurl> www.abc.com.cn </siteurl>
<Sitedescript>. Net Open Source Forum </sitedescript>
<Sitekeyword> XML operation example </sitekeyword>
<Filetype> GIF | JPG | RAR </filetype>
& Lt; filesize & gt; 1024 & lt;/filesize & gt;
<Filters> script | IFRAME </Filters>
<Filterevent> onload | onmouseover </filterevent>
<Badwords> admin | webmaster | drbbs </badwords>
<Disallowip> 0.0.0.0 </disallowip>
<Isclosereg> false </isclosereg>
<Isclosebbs> false </isclosebbs>
<Postpay> 5 </postpay>
<Replypay> 2 </replypay>
<Goodpay> 10 </goodpay>
<Delpost> 5 </delpost>
<Delreply> 2 </delreply>
<Canclegood> 10 </canclegood>
</Drroot>

2 main methods

Public String readvalue (string key)
{< br> xmldocument XD = new xmldocument ();
XD. load (server. mappath ("model. XML ");
xmlnodelist xnl = XD. getelementsbytagname (key);
If (xnl. count = 0)
return "";
else
{< br> xmlnode mnode = xnl [0];
return mnode. innertext;
}< BR >}

Public void updatevalue (string nkey, string nvalue)
{
If (readvalue (nkey )! = "")
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath ("model. xml "));
Xmlnodelist elemlist = xmldoc. getelementsbytagname (nkey );
Xmlnode mnode = elemlist [0];
Mnode. innertext = nvalue;
Xmltextwriter XW = new xmltextwriter (New streamwriter (server. mappath ("model. xml ")));
XW. Formatting = formatting. indented;
Xmldoc. writeto (XW );
XW. Close ();
}
}

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.