. NET operations XML (1) Add, delete, modify, query

Source: Internet
Author: User

Query XML This is the XML that I get the foreground menu

private void GetMenu (string[] cidlist, string[] idlist)
{
Get menu File
String spath = System.Web.HttpContext.Current.Request.MapPath ("Menu/menufile.xml");
Load
Xml. Load (spath);
for (int i = 0; i < cidlist. Length; i++)
{
XmlNode Thisnode = XML. Documentelement.selectsinglenode ("/menus/menu[@cid =" + cidlist[i]. ToString () + "]");
if (Thisnode! = null)
{
HTML + = "<li class= ' ><a href= ' javascript:; ' ><i class= ' "+ thisnode.attributes[" icon "]. Value + "' ></i> <span class= ' title ' >" + thisnode.attributes["heard"]. Value + "</span><span class= ' arrow ' ></span></a><ul class= ' sub-menu ' >";
for (int j = 0; J < Idlist. Length; J + +)
{
XmlNode thisNode1 = XML. Documentelement.selectsinglenode ("/menus/menu[@cid =" + cidlist[i]. ToString () + "]/li[@id =" + idlist[j]. ToString () + "]");
if (thisNode1! = null)
{
HTML + = "<li><a onclick= ' gotosrc (this) ' src= '" + thisnode1.attributes["src"]. Value + "' target= ' main ' >" + thisnode1.attributes["tittle"]. Value + "</a></li>";
}
Else
{
Continue
}
}
HTML + = "</ul></li>";
}
}
}

Add to:

String name = Context. request.form["Name"]. ToString ();
String strcid = context. request.form["Strcid"]. ToString ();
String Strtid = context. request.form["Strtid"]. ToString ();
XmlDocument xml = new XmlDocument ();
String spath = System.Web.HttpContext.Current.Request.MapPath (".. /menu/rolefile.xml ");
XML. Load (spath);
XmlNode root = XML. selectSingleNode ("Roles");
XmlNodeList xnl = root. ChildNodes;
BOOL flag = FALSE;
foreach (XmlNode item in XNL)
{
XmlElement xe = (XmlElement) item;
if (XE. GetAttribute ("roleName") = = name)
{
flag = true;
}
Else
{
continue;
}
}
if (flag = = False)
{
XmlElement xe1 = XML. createelement ("role");
Xe1. SetAttribute ("RoleName", name);
Xe1. SetAttribute ("CID", strcid);
Xe1. SetAttribute ("PList", Strtid);
Root. AppendChild (XE1);
XML. Save (spath);
Context. Response.Write ("OK");
}
Else
{
context. Response.Write ("This role already exists!");
}

Delete:

String roleName = context. request.form["RoleName"]. Trim (). ToString ();
String spath = System.Web.HttpContext.Current.Request.MapPath (".. /menu/rolefile.xml ");
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (spath);
XmlNodeList XNL = Xmldoc.selectsinglenode ("Roles"). ChildNodes;
String flag = "NO";
foreach (XmlNode xn in xnl)
{
XmlElement XE = (XmlElement) xn;
if (XE. GetAttribute ("roleName") = = RoleName)
{
Xe. RemoveAll ();//delete the entire contents of the node
Xn. RemoveAll (); Delete the node
Flag = "OK";
Break
}
}
if (flag = = "OK")
{
Xmldoc.save (spath);
Context. Response.Write (flag);
}
else {
Context. Response.Write (flag);
}

Modify and add the same is to re-assign a value to save the document

. NET operations XML (1) Add, delete, modify, query

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.