xml 文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book genre="fantasy" ISBN="4-2341-8">
<title>C#编程指南</title>
<author>James Zhao aaa</author>
<price>53.95</price>
</book>
<book genre="计算机" ISBN="4-12344-8">
<title>C#入门经典</title>
<author>不知道</author>
<price>20$</price>
</book>
<book genre="xiaotuni" ISBN="2-3342-8">
<title>C#高级编程</title>
<author>LHB</author>
<price>13$</price>
</book>
<book genre="asp.net" ISBN="7-302-05407-x">
<title>ASP.NET入门经典</title>
<author>Chris Ullman、Chirs Goode and so on</author>
<price>75元</price>
</book>
<book genre="廖海兵" ISBN="JX-XY-1984-03-08">
<title>我的生日</title>
<author>xiaotuni</author>
<price>21</price>
</book>
<book genre="a" ISBN="b">
<title>c</title>
<author>d</author>
<price>e</price>
</book>
<book genre="aa" ISBN="BB-CC-DD-EE-FF">
<title>goole Cools</title>
<author>不明</author>
<price>20元</price>
</book>
<book genre="b" ISBN="BB-E-1234-JX">
<title>KSIK</title>
<author>LASERF</author>
<price>20元</price>
</book>
<book genre="www" ISBN="baidu">
<title>com</title>
<author>1234</author>
<price>21</price>
</book>
</bookstore>
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book genre="fantasy" ISBN="4-2341-8">
<title>C#编程指南</title>
<author>James Zhao aaa</author>
<price>53.95</price>
</book>
<book genre="计算机" ISBN="4-12344-8">
<title>C#入门经典</title>
<author>不知道</author>
<price>20$</price>
</book>
<book genre="xiaotuni" ISBN="2-3342-8">
<title>C#高级编程</title>
<author>LHB</author>
<price>13$</price>
</book>
<book genre="asp.net" ISBN="7-302-05407-x">
<title>ASP.NET入门经典</title>
<author>Chris Ullman、Chirs Goode and so on</author>
<price>75元</price>
</book>
<book genre="廖海兵" ISBN="JX-XY-1984-03-08">
<title>我的生日</title>
<author>xiaotuni</author>
<price>21</price>
</book>
<book genre="a" ISBN="b">
<title>c</title>
<author>d</author>
<price>e</price>
</book>
<book genre="aa" ISBN="BB-CC-DD-EE-FF">
<title>goole Cools</title>
<author>不明</author>
<price>20元</price>
</book>
<book genre="b" ISBN="BB-E-1234-JX">
<title>KSIK</title>
<author>LASERF</author>
<price>20元</price>
</book>
<book genre="www" ISBN="baidu">
<title>com</title>
<author>1234</author>
<price>21</price>
</book>
</bookstore>
/// <summary>
/// 添加数据到Xml里去
/// </summary>
/// <param name="Gener">类型</param>
/// <param name="isbn">图书编号</param>
/// <param name="Author">作者</param>
/// <param name="Title">图书标题</param>
/// <param name="Price">价格</param>
private bool AddDate(string Genre,string isbn,string Author,string Title,string Price)
{
bool addXmlDate = false; //返回是不是要添加数据
int ChunZai = 0; //记录是不是存在
int BuChunZai = 0; //记录是不是不存在
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("BookStore.xml")); //把XML文件装载进来
//把有的BookStore节点的所有子节点拿出来
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
//遍历第一级所有子节点
foreach (XmlNode xn in nodeList)
{
//
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == Genre)//说明此已经存在
{
ChunZai++;
break;
}
else
{
BuChunZai++;
if (BuChunZai == nodeList.Count)
{
break;
}
}
}
if (ChunZai == 0) //如果存在为0那么说明此xml文件里没有此数据可以添加
{
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
XmlElement xel = xmlDoc.CreateElement("book");
xel.SetAttribute("genre", Genre);
//设置该节点的ISBN属性
xel.SetAttribute("ISBN", isbn);
//设置本节点为书的标题
XmlElement xesubTitle = xmlDoc.CreateElement("title");
xesubTitle.InnerText = Title;
xel.AppendChild(xesubTitle);
//书作者
XmlElement xesubAuthor = xmlDoc.CreateElement("author");
xesubAuthor.InnerText = Author;
xel.AppendChild(xesubAuthor);
//书价格
XmlElement xesubPrice = xmlDoc.CreateElement("price");
xesubPrice.InnerText = Price;
xel.AppendChild(xesubPrice);
//添加到 bookStore 的root里去
root.AppendChild(xel);
xmlDoc.Save(Server.MapPath("BookStore.xml")); //保存添加的数据
addXmlDate = true;
}
else
{
addXmlDate = false;//说明要添加的数据已经存在,
}
return addXmlDate;
}
/// 添加数据到Xml里去
/// </summary>
/// <param name="Gener">类型</param>
/// <param name="isbn">图书编号</param>
/// <param name="Author">作者</param>
/// <param name="Title">图书标题</param>
/// <param name="Price">价格</param>
private bool AddDate(string Genre,string isbn,string Author,string Title,string Price)
{
bool addXmlDate = false; //返回是不是要添加数据
int ChunZai = 0; //记录是不是存在
int BuChunZai = 0; //记录是不是不存在
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("BookStore.xml")); //把XML文件装载进来
//把有的BookStore节点的所有子节点拿出来
XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
//遍历第一级所有子节点
foreach (XmlNode xn in nodeList)
{
//
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == Genre)//说明此已经存在
{
ChunZai++;
break;
}
else
{
BuChunZai++;
if (BuChunZai == nodeList.Count)
{
break;
}
}
}
if (ChunZai == 0) //如果存在为0那么说明此xml文件里没有此数据可以添加
{
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
XmlElement xel = xmlDoc.CreateElement("book");
xel.SetAttribute("genre", Genre);
//设置该节点的ISBN属性
xel.SetAttribute("ISBN", isbn);
//设置本节点为书的标题
XmlElement xesubTitle = xmlDoc.CreateElement("title");
xesubTitle.InnerText = Title;
xel.AppendChild(xesubTitle);
//书作者
XmlElement xesubAuthor = xmlDoc.CreateElement("author");
xesubAuthor.InnerText = Author;
xel.AppendChild(xesubAuthor);
//书价格
XmlElement xesubPrice = xmlDoc.CreateElement("price");
xesubPrice.InnerText = Price;
xel.AppendChild(xesubPrice);
//添加到 bookStore 的root里去
root.AppendChild(xel);
xmlDoc.Save(Server.MapPath("BookStore.xml")); //保存添加的数据
addXmlDate = true;
}
else
{
addXmlDate = false;//说明要添加的数据已经存在,
}
return addXmlDate;
}