学习目标
1.获取一个节点,并转化为一个实体对象
获取根节点对象->获得子节点列表->遍历匹配->唯一查找
public Model.url_rewrite GetInfo(string attrValue)
{
Model.url_rewrite model = new Model.url_rewrite();
string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode xn = doc.SelectSingleNode("urls");//获取根节点对象
XmlNodeList xnList = xn.ChildNodes;//获得子节点列表
if (xnList.Count > 0)//遍历匹配查找
{
foreach (XmlElement xe in xnList)
{
if (xe.Attributes["name"].Value.ToLower() == attrValue.ToLower())//name是唯一的
{
model.name = xe.Attributes["name"].Value;
model.path = xe.Attributes["path"].Value;
model.pattern = xe.Attributes["pattern"].Value;
model.page = xe.Attributes["page"].Value;
model.querystring = xe.Attributes["querystring"].Value;
model.templet = xe.Attributes["templet"].Value;
model.channel = xe.Attributes["channel"].Value;
model.type = xe.Attributes["type"].Value;
model.inherit = xe.Attributes["inherit"].Value;
return model;
}
}
}
return null;
}
2.增加一个节点
加载XML文件--获得父节点urls--创建元素--设置属性--追加节点--保存xml文件
/// <summary>
/// 增加节点
/// </summary>
public bool Add(Model.url_rewrite model)
{
try
{
string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode xn = doc.SelectSingleNode("urls");
XmlElement xe = doc.CreateElement("rewrite");
xe.SetAttribute("name", model.name);
xe.SetAttribute("path", model.path);
xe.SetAttribute("pattern", model.pattern);
xe.SetAttribute("page", model.page);
xe.SetAttribute("querystring", model.querystring);
xe.SetAttribute("templet", model.templet);
xe.SetAttribute("channel", model.channel.ToString());
xe.SetAttribute("type", model.type);
xe.SetAttribute("inherit", model.inherit);
xn.AppendChild(xe);
doc.Save(filePath);
return true;
}
catch
{
return false;
}
}
3.修改节点
操作的流程大致都差不多
/// <summary>
/// 修改节点
/// </summary>
public bool Edit(Model.url_rewrite model)
{
string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode xn = doc.SelectSingleNode("urls");
//子节点列表
XmlNodeList xnList = xn.ChildNodes;
if (xnList.Count > 0)
{
foreach (XmlElement xe in xnList)
{
//遍历查找
if (xe.Attributes["name"].Value.ToLower() == model.name.ToLower())
{
//设置属性
xe.Attributes["path"].Value = model.path;
xe.Attributes["pattern"].Value = model.pattern;
xe.Attributes["page"].Value = model.page;
xe.Attributes["querystring"].Value = model.querystring;
xe.Attributes["templet"].Value = model.templet;
xe.Attributes["channel"].Value = model.channel.ToString();
xe.Attributes["type"].Value = model.type;
xe.Attributes["inherit"].Value = model.inherit;
doc.Save(filePath);
return true;
}
}
}
return false;
}
4.删除节点
/// <summary>
/// 删除节点
/// </summary>
public bool Remove(string attrName, string attrValue)
{
string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode xn = doc.SelectSingleNode("urls");
XmlNodeList xnList = xn.ChildNodes;
if (xnList.Count > 0)
{
for (int i = xnList.Count - 1; i >= 0; i--)
{
XmlElement xe = (XmlElement)xnList.Item(i);
if (xe.Attributes[attrName].Value.ToLower() == attrValue.ToLower())
{
xn.RemoveChild(xe);//从父节点中移除子节点
}
}
doc.Save(filePath);
return true;
}
return false;
}