ASP. NET reads and deletes configuration files (including parent nodes ).

Source: Internet
Author: User

<? XML version = "1.0" encoding = "gb2312"?>
<Root>
<Comment>
<URL> http://image.meimumeijia.com/product_comment/2013/08/327956_201308161103132272_small.jpg </URL>
<DESC> test the function! </DESC>
</Comment>
<Comment>
<URL> http://image.meimumeijia.com/product_comment/2013/08/327956_201sdfsdf272_small.jpg </URL>
<DESC> test and test sdfs! </DESC>
</Comment>
</Root>

/// <Summary>

/// Create and add an XML Node
/// </Summary>
/// <Param name = "url"> image link </param>
/// <Param name = "DESC"> description </param>
Public void createxmlnode (string URL, string DESC)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (system. Web. httpcontext. Current. server. mappath ("/") + "Product/comment. xml ");

Xmlnode root = xmldoc. selectsinglenode ("root ");
Xmlelement xe1 = xmldoc. createelement ("comment ");

Xmlelement xesub1 = xmldoc. createelement ("url ");
Xesub1.innertext = URL;
Xe1.appendchild (xesub1 );

Xmlelement xesub2 = xmldoc. createelement ("DESC ");
Xesub2.innertext = DESC;
Xe1.appendchild (xesub2 );

Root. appendchild (xe1 );
Xmldoc. Save (system. Web. httpcontext. Current. server. mappath ("/") + "Product/comment. xml ");

}
/// <Summary>
/// Delete the first subnode of the XML file
/// </Summary>
Public void deletexmlnode ()
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (system. Web. httpcontext. Current. server. mappath ("/") + "Product/comment. xml ");
Xmlnodelist xnl = xmldoc. selectsinglenode ("root"). childnodes;
If (xnl. Count <5)
{
Return;
}
Foreach (xmlnode Xn in xnl)
{
If (xnl. Item (0) = xn)
{
XN. parentnode. removechild (Xn); // delete a node (including the secondary Node itself)
Break;
}
}

Xmldoc. Save (system. Web. httpcontext. Current. server. mappath ("/") + "Product/comment. 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.