111,120
社区成员
发帖
与我相关
我的任务
分享<MenuItem text="文件">
<MenuItem text="新建">
<MenuItem>项目</Grandson>
<MenuItem>网站</Grandson>
<MenuItem text="文件">
<MenuItem>文件1</MenuItem>
<MenuItem>文件2</MenuItem>
</MenuItem>
</MenuItem>
<MenuItem text="打开">
<MenuItem>项目</MenuItem>
</MenuItem>
</MenuItem>
/// <summary>
/// 删除属性带key的节点,key:GUID
/// </summary>
/// <param name="path">XML文件路径</param>
/// <param name="rootnode">根节点</param>
/// <param name="key">key:Guid</param>
/// <param name="msg">返回的信息</param>
public static void DeleteXMLNode(string path, string rootnode, string znode,string type,string key)
{
try
{
if (System.IO.File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList xl = xmlDoc.SelectSingleNode(rootnode).ChildNodes;
for (int i = 0; i < xl.Count; i++)
{
XmlElement xe = (XmlElement)xl[i];//第i个dbGust子节点
XmlNodeList node = xe.GetElementsByTagName(znode);
if (node.Count > 0)
{
for (int j = 0; j < node.Count; j++)
{
XmlNode node1 = node.Item(j);
XmlElement xe1 = (XmlElement)node.Item(j);
if (xe1.GetAttribute(type) == key)
{
xmlDoc.SelectSingleNode(rootnode).RemoveChild(node[0].ParentNode);//删除该节点
break;
}
}
}
}
xmlDoc.Save(path);
}
}
catch (XmlException ex)
{
}
}
/// <summary>
/// 返回符合指定名称的节点数
/// </summary>
/// <param name="nodeName">节点名</param>
/// <returns>节点数</returns>
public static int Count(string path,string nodeName,string znode)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
XmlNodeList nodeList = xe.GetElementsByTagName(znode);
return nodeList.Count;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 返回最后的数字
/// </summary>
/// <param name="path"></param>
/// <param name="nodeName"></param>
/// <param name="znode"></param>
/// <returns></returns>
public static string num(string path, string nodeName, string znode,string type)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
XmlNodeList nodeList = xe.GetElementsByTagName(znode);
int i = nodeList.Count;
XmlElement xe1 = (XmlElement)nodeList.Item(nodeList.Count-1);
string inum=xe1.GetAttribute(type);
return inum;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
}
}
using System;
using System.Data;
using System.Configuration;
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;
using System.Collections.Generic;
namespace NetView.Models
{
/// <summary>
/// XmlHelper 的摘要说明
/// </summary>
public class XmlHelper
{
private string msg;
public XmlHelper()
{
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/**************************************************
* 使用示列:
* XmlHelper.Read(path, "/Node", "")
* XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
************************************************/
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
}
/// <summary>
/// 插入
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="node1"></param>
/// <param name="element"></param>
/// <param name="attribute"></param>
/// <param name="value"></param>
public static void Insert(string path, string node, string node1, string element, string attribute, string value, string attribute1, string value1)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node); //查找根node节点
XmlElement xel = doc.CreateElement(node1);
//if (element.Equals(""))
//{
// if (!attribute.Equals(""))
// {
// XmlElement xe = (XmlElement)xe1;
// xe.SetAttribute(attribute, value);
// }
//}
//else
//{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xe.SetAttribute(attribute1, value1);
xel.AppendChild(xe);
xn.AppendChild(xel);
//}
doc.Save(path);
}
catch { }
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path, "/Node", "Element", "", "Value")
* XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
* XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
************************************************/
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path, "/Node", "", "Value")
* XmlHelper.Insert(path, "/Node", "Attribute", "Value")
************************************************/
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
doc.Save(path);
}
catch { }
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Delete(path, "/Node", "")
* XmlHelper.Delete(path, "/Node", "Attribute")
************************************************/
public static void Delete(string path, string node, string attribute)
{
// XmlDocument doc = new XmlDocument();
// doc.Load(path);
// XmlNode xn = doc.SelectSingleNode(node);
// string mainNode = node.Substring(0, node.LastIndexOf("/"));
//doc.SelectSingleNode(mainNode).RemoveChild(doc.SelectSingleNode(node));
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
// XmlNode xn = doc.SelectSingleNode(node);
XmlNodeList xnl = doc.SelectSingleNode(node).ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlNodeList xnl1 = xn.SelectSingleNode("RedirectUrl").ChildNodes;
XmlElement xe = (XmlElement)xn;
for (int j = 0; j < xnl1.Count; j++)
{
XmlElement xe1 = (XmlElement)xnl1.Item(j);
if (xe1.GetAttribute("UrlName") == "小路工作室1231231231")
{
xn.RemoveChild(xe1);
if (j < xnl1.Count) j = j - 1;
}
}
// if (xe.GetAttribute("genre") == "fantasy")
//{
// xe.RemoveAttribute("genre");//删除genre属性
// }
// else if (xe.GetAttribute("genre") == "update李赞红")
//{
// xe.RemoveAll();//删除该节点的全部内容
// }
}
//for (int i = 0; i < xnl.Count; i++)
//{
// XmlNodeList xnl1 = xnl.Item(i).SelectSingleNode("RedirectUrl").ChildNodes;
// for (int j = 0; j < xnl1.Count; j++)
// {
// XmlElement xe = (XmlElement)xnl1.Item(j);
// if (xe.GetAttribute("UrlName") == "小路工作室1231231231")
// {
// xn.RemoveChild(xe);
// if (i < xnl1.Count) i = i - 1;
// }
// }
//}
//XmlElement xe = (XmlElement)xn;
//if (attribute.Equals(""))
// xn.ParentNode.RemoveChild(xn);
//else
// xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch { }
}
public static string ReadXml(string strKey)//写入动态的数据库配置信息
{
string TempValues = "";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
att = nodes[i].Attributes["value"];
TempValues = att.Value;
break;
}
}
doc.Save(strFileName);
return TempValues;
}
XmlDocument xml = new XmlDocument();
xml.Load("Xml文件地址");
XmlNode node = xml.SelectSingleNode("Menu");
if(node != null)
{
foreach(XmlNode n in node.ChildNodes)
{
//...............
}
}