A simple C # class for operating XML files. Only one layer of nodes can be operated.

Source: Internet
Author: User

Using system;

Using system. xml;

 

Namespace windowsformsapplication3

{

Class xmlcontrol

{

Xmldocument xmldoc; // XML file object

Xmlelement; // XML Element

Xmltext; // XML text

Xmlnodelist xmllist; // XML list

String Surl = "\ dbset. xml ";

Public xmlcontrol ()

{

Xmldoc = new xmldocument ();

Xmldoc. Load ("\ dbset. xml ");

}

Public xmlcontrol (string sxmlurl) // xmlurl is the path of the XML file

 

No deletion method

{

Xmldoc = new xmldocument ();

Xmldoc. Load (sxmlurl );

Surl = sxmlurl;

}

Public bool addnode (string snodename, string snodevalue) // Add a node

{

Try

{

Xmlelement = xmldoc. createelement (snodename );

Xmltext = xmldoc. createtextnode (snodevalue );

Xmlelement. appendchild (xmltext );

Xmldoc. childnodes. Item (1). appendchild (xmlelement );

Xmldoc. Save (Surl );

Return true;

}

Catch

{

Return false;

}

}

Public bool updatanodevalue (string snodename, string snodevalue)

{

Try

{

Xmllist = xmldoc. getelementsbytagname (snodename );

Xmllist [0]. innertext = snodename;

Xmldoc. Save (Surl );

Return true;

}

Catch

{

Return false;

}

}

Public String selectnode (string snodename)

{

String snodevalue = NULL;

Try

{

Xmllist = xmldoc. selectnodes ("// setting/" + snodename );

Foreach (xmlnode xl in xmllist)

{

Snodevalue = XL. innertext. tostring ();

}

Return snodevalue;

}

Catch

{

Return NULL;

}

}

}

}

 

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.