在asp.net添加数据到XML里去
来源:互联网 发布:淘宝卖家大尺度自拍照 编辑:程序博客网 时间:2024/05/27 09:46
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>
那个html里的代码就不知道怎么弄,好几个月了,代码不知道放到哪里去了
<?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;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1348617
- 在asp.net添加数据到XML里去
- 在asp.net添加数据到XML里去
- asp.net里怎么提交数据到另外一页去
- 在ASP.net里添加 javascript
- 将Asp.Net页面输出到EXCEL里去
- 将Asp.Net页面输出到EXCEL里去
- 将Asp.Net页面输出到EXCEL里去
- 将Asp.Net页面输出到EXCEL里去
- 将Asp.Net页面输出到EXCEL里去
- 转: 将Asp.Net页面输出到EXCEL里去
- 将Asp.Net页面输出到EXCEL里去
- ASP.net从数据库中取出数据添加到xml文件中的方法
- ASP.NET从数据库中取出数据添加到xml中方法
- 在ASP.NET中绑定XML数据
- 在asp.net中 把一个文本文档的中的数据一行一行添加到textbox中
- xml + asp.net 实现 xml数据读取到 treeview
- 通过数据捆绑将数据添加到ASP.NET 页面
- Scott Mitchell 的ASP.NET 2.0数据教程之三十九:: 在编辑和插入界面里添加验证控件
- Java Servlet API类库中的基本方法介绍
- [英语歌曲]蝴蝶之吻:Butterfly kisses
- ajax 笔记--不用刷新实现数据的分页显示 2 (上)
- 高性能、高弹性JSP和Servlet性能优化
- 四级过了,呵呵...
- 在asp.net添加数据到XML里去
- 第一次用javascript写了一个不用刷新就能实现--用户名验证的例子
- ddd
- 魔羯的爱情
- 长发
- 几个常用得事务处理方法
- JavaScript高级程序设计 读书笔记(一)
- GridView 隐藏某中得些列.并在HeadText上显示相关的信息
- 关于是否要考研