早几天做了一个用xml文件存储友情链接的程序,xml文件内容如下:
<?xml version="1.0" encoding="gb2312"?>
<root>
<link id="1">
<linkid>1</linkid>
<sitename>华中科技大学东莞研究院 </sitename>
<linkurl>http://www.dghust.com</linkurl>
</link>
<link id="2">
<linkid>2</linkid>
<sitename>e-works </sitename>
<linkurl>http://www.e-works.net.cn</linkurl>
</link>
<link id="3">
<linkid>3</linkid>
<sitename>新浪 </sitename>
<linkurl>http://www.sina.com.cn</linkurl>
</link>
</root>
下面代码实现了增加、修改、删除友情链接
private void CreateLinkXml(string sitename, string linkurl)
{
string xmlFilePath = "xml/friendlink.xml";
string xmlFile = Server.MapPath(xmlFilePath);
XmlDocument xmlDoc = new XmlDocument();
if (!File.Exists(xmlFile))//创建xml文件
{//创建xml文件
XmlTextWriter objX = new XmlTextWriter(xmlFile, System.Text.Encoding.GetEncoding("GB2312"));
objX.Formatting = Formatting.Indented;
objX.WriteStartDocument();
objX.WriteStartElement("root");//创建根目录
objX.WriteEndElement();
objX.Close();
}
xmlDoc.Load(xmlFile);
XmlNode e_rss = xmlDoc.SelectSingleNode("root");
int i ;
XmlNode lastNode = e_rss.LastChild;
if (lastNode != null)
{
i = int.Parse(lastNode.FirstChild.InnerText) + 1;//将最后一个节点linkid的值加1赋给新加的节点linkid的值
}
else
i = 1;
//创建新的节点元素link
XmlElement e_node = xmlDoc.CreateElement("link");
e_node.SetAttribute("id", i.ToString());
XmlElement e_linkid = xmlDoc.CreateElement("linkid");
e_linkid.InnerText = i.ToString();
e_node.AppendChild(e_linkid);//在link节点下新加linkid子节点
XmlElement e_title = xmlDoc.CreateElement("sitename");
e_title.InnerText = sitename;
e_node.AppendChild(e_title);//在link节点下新加sitename子节点
XmlElement e_link = xmlDoc.CreateElement("linkurl");
e_link.InnerText = linkurl;
e_node.AppendChild(e_link);//在link节点下新加linkurl子节点
e_rss.AppendChild(e_node);
xmlDoc.Save(xmlFile);
xmlDoc = null;
}
//编辑
private void ModifyXml(string linkId, string sitename, string linkurl)
{
XmlDocument xmlDoc = new XmlDocument();
string xmlFile = Server.MapPath(xmlFilePath);
xmlDoc.Load(xmlFile);
//XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link/linkid[text()='"+linkId+"']");
//找到link节点属性值等于linkId的节点
XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link[@id='" + linkId + "']");
if (nodecp != null)
{
XmlNodeList nodlist = nodecp.ChildNodes;
if (nodlist != null)
{
foreach (XmlNode MyNode in nodlist)
{
if (MyNode.Name == "sitename")
{
MyNode.InnerText = sitename;
}
if (MyNode.Name == "linkurl")
{
MyNode.InnerText = linkurl;
}
}
}
}
xmlDoc.Save(xmlFile);
xmlDoc = null;
}
//删除
private void DeleteXml(string linkId)
{
XmlDocument xmlDoc = new XmlDocument();
string xmlFile = Server.MapPath(xmlFilePath);
xmlDoc.Load(xmlFile);
XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link[@id='" + linkId + "']");
XmlNode parent = nodecp.ParentNode;
parent.RemoveChild(nodecp);
//nodecp.RemoveAll();
xmlDoc.Save(xmlFile);
xmlDoc = null;
}
/// <summary>
/// 根据xml节点属性找到节点另一个属性值
/// </summary>
/// <param name="valuestring">节点的已知属性值</param>
/// <param name="xmlFile">xml文件(包括路径)</param>
/// <param name="rootnode">节点路径</param>
/// <param name="attr">已知属性名</param>
/// <param name="valuename">想得到值的属性名</param>
/// <returns></returns>
public static string GetXmlNodeValue(string valuestring, string xmlFile, string rootnode,string attr,string valuename)
{
XmlDocument xmlDoc = new XmlDocument();
xmlFile =System.Web.HttpContext.Current.Server.MapPath(xmlFile);
xmlDoc.Load(xmlFile);
XmlNode nodecp = xmlDoc.SelectSingleNode(""+rootnode+"[@"+attr+"='" + valuestring + "']");
return nodecp.Attributes[""+valuename+""].Value;
}
/// <summary>
/// 搜索xml含有特定字符的属性节点的另一属性值
/// </summary>
/// <param name="valuestring">输入的字符</param>
/// <param name="xmlFile">xml文件(包括路径)</param>
/// <param name="rootnode">xml根节点</param>
/// <param name="attr">搜索是否含有字符的属性名</param>
/// <param name="valuename">想得到值的属性名</param>
/// <returns></returns>
public static string SerarchXmlValue(string valuestring, string xmlFile, string rootnode,string attr,string valuename)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(xmlFile));
XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlElement element in topM)
{
if (element.Name.ToLower() == rootnode)
{
//得到该节点的子节点集
XmlNodeList nodelist = element.ChildNodes;
if (nodelist.Count > 0)
{
foreach (XmlElement el in nodelist)//读元素值
{
if (el.Attributes[""+attr+""].Value.IndexOf(valuestring)!=-1)
{
return el.Attributes[""+valuename+""].Value;
}
}
}
}
}
return "";
}