用xml做数据库的操作

dongdong19871024 2009-05-08 10:47:32
asp.net,用xml做数据库,应该怎样操作??像ado.net操作sqlserver或access一样,包括数据源的绑定,删除,添加,修改

举个简单的例子,留言板程序,数据库包含两个表,一个是用户表,一个是用户的留言表

用户表的XML文件如下
<?xml version="1.0" encoding="utf-8"?>
<xml_user>
<user id="1">
<username>admin</username>
<userpwd>admin</userpwd>
</user>
<user id="2">
<username>dong</username>
<userpwd>dong</userpwd>
</user>
<user id="3">
<username>dongdong</username>
<userpwd>dongdong</userpwd>
</user>
</xml_user>
留言表类不列出来了


GRIDVIEW 或者DATALIST绑定XML数据源,单击留言可以查看详细信息,添加留言之类的 应该怎样操作?

知道的朋友帮下忙,举个简单的例子也行
...全文
418 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdg个sggggg 2010-05-25
  • 打赏
  • 举报
回复
lz换个头像吧
gzchenbing 2009-05-14
  • 打赏
  • 举报
回复
把xml的信息读取存放在集合中,再赋值到GRIDVIEW .DataSource就可以啦
alan817 2009-05-14
  • 打赏
  • 举报
回复
把user节点序列化为 list
绑定也方便
myrroom 2009-05-14
  • 打赏
  • 举报
回复
顶上去
>>>>>>>>>>------------------------------------------------------------------------------------------<<<<<<<<<<
saisky 2009-05-14
  • 打赏
  • 举报
回复
Xml装载到DataSet中
然后从表里读取各列数据
xiaojing7 2009-05-09
  • 打赏
  • 举报
回复
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>

1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
//===============================================
结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>

2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”

XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//==================================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>

3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//===========================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>

4、显示所有数据。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

XmlNodeList xnl=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));

XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
  • 打赏
  • 举报
回复
将创建一个实体类User,也就是为了在gridview或repeater中更方便的绑定的。
对gridview中的模板列的绑定当然也没有什么问题。

查询的话就使用xpath.
分页的话,如果数据量不大,可以考虑直接使用gridview的分页功能.

dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 duping9626 的回复:]
引用 7 楼 dongdong19871024 的回复:
先谢谢你,这样写的话,应该就不能对gridview绑定字段了 hyperLink也应该绑定不了连接地址了吧??

有别的方式吗?这样写的话,显示内容很方便,操作起来很麻烦。

查询代码应该怎样写?用Xpath??还有分页....

可以绑定,字段就是user类的属性
分页是你GridView设的, AllowPaging
直接操作Xml文件的查询可以用 XPathNavigator
[/Quote]
好的 我试试 谢谢
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zhouyongli41 的回复:]
6楼class user
这就是实体化
[/Quote]

哦 那就明白了,不过还是那个问题,对分页和查询应该怎样操作?(最好是分页无刷新)
duping9626 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dongdong19871024 的回复:]
先谢谢你,这样写的话,应该就不能对gridview绑定字段了 hyperLink也应该绑定不了连接地址了吧??

有别的方式吗?这样写的话,显示内容很方便,操作起来很麻烦。

查询代码应该怎样写?用Xpath??还有分页....
[/Quote]
可以绑定,字段就是user类的属性
分页是你GridView设的, AllowPaging
直接操作Xml文件的查询可以用 XPathNavigator
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yukon12345 的回复:]
其实把xml读到datase里之后就相当于几个表
XmlDataDocument xdoc=new XmlDataDocument();
xdoc.dataset.ReadXml("路径");

xml文件:
<?xml version="1.0" encoding="utf-8"?>
<xml_user>
<user id="1">
<username>admin </username>
<userpwd>admin </userpwd>
</user>
<user id="2">
<username>dong </username>
<userpwd>dong </userpwd>
</user>
<user id="3">

[/Quote]

如果要做查找和分页,这样用dataset不是会很麻烦吗~!
zhouyongli41 2009-05-08
  • 打赏
  • 举报
回复
6楼class user
这就是实体化
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhouyongli41 的回复:]
最好实体化类,操作方便
[/Quote]

不太明白~~~
zhouyongli41 2009-05-08
  • 打赏
  • 举报
回复
最好实体化类,操作方便
步慢生错 2009-05-08
  • 打赏
  • 举报
回复
其实把xml读到datase里之后就相当于几个表
XmlDataDocument xdoc=new XmlDataDocument();
xdoc.dataset.ReadXml("路径");

xml文件:
<?xml version="1.0" encoding="utf-8"?>
<xml_user>
<user id="1">
<username>admin </username>
<userpwd>admin </userpwd>
</user>
<user id="2">
<username>dong </username>
<userpwd>dong </userpwd>
</user>
<user id="3">
<username>dongdong </username>
<userpwd>dongdong </userpwd>
</user>
</xml_user>
相当于dataset.Tables[0]为xmluser表,dataset.Tables[1]为user表,user表下有id username 和userpwd列。
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
先谢谢你,这样写的话,应该就不能对gridview绑定字段了 hyperLink也应该绑定不了连接地址了吧??

有别的方式吗?这样写的话,显示内容很方便,操作起来很麻烦。

查询代码应该怎样写?用Xpath??还有分页....

  • 打赏
  • 举报
回复
简单的写了一个查询

namespace testWebProject
{
public partial class index : System.Web.UI.Page
{
public class user
{
private string userid;

public string Userid
{
get { return userid; }
set { userid = value; }
}

private string username;

public string Username
{
get { return username; }
set { username = value; }
}

private string userpwd;

public string Userpwd
{
get { return userpwd; }
set { userpwd = value; }
}

public user(string userid, string username, string userpwd)
{
this.userid = userid;
this.username = username;
this.userpwd = userpwd;
}


}

protected void Page_Load(object sender, EventArgs e)
{
List<user> userCollection = new List<user>();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath("~/XMLFile1.xml"));
System.Xml.XmlNodeList collection = doc.SelectNodes("/xml_user/user");
foreach (System.Xml.XmlNode node in collection)
{
userCollection.Add(new user(node.Attributes["id"].Value, node.ChildNodes[0].InnerText, node.ChildNodes[1].InnerText));
}
GridView1.DataSource = userCollection;
GridView1.DataBind();
}
}
}
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 coodd 的回复:]
xml的数据库,数据一多那得多慢,不建议用。
[/Quote]

只是想做个练习,老用sqlserver和access,换着学习下。 之前看过XML的书,AJAX也是刚刚入门,希望大家指导下
coodd 2009-05-08
  • 打赏
  • 举报
回复
xml的数据库,数据一多那得多慢,不建议用。
dongdong19871024 2009-05-08
  • 打赏
  • 举报
回复
读取到DATASET中,就能像对sqlserver这类的数据库一样的操作??

对数据筛选,比如说时间,我想要查看一定时间内的留言,还有排序,应该怎样做?


希望能给个小例子或者简单的写一段代码,谢谢大家
加载更多回复(2)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧