在项目中建立一个 example.xml文件数据库里有Document字段是xml类型的如何将数据库中一条记录的Document字段值 加入到example.xml文件中谢谢大家!!
解决方案 »
- 求怎么解码?
- 类型转换
- office 2007 中的Ribbon如何翻译?
- 呵呵。。大家在工作之余听听 回忆2005 保定方言版,挺搞笑的
- asp.net c# gridview 导出到excel问题(高分)
- 各位帮帮忙
- C#.NET的WINDOWS应用程序,用什么来记录登陆名和用户密码?如一些管理系统,注册后,这些东西放在哪呢?
- listview和treeview图标有时候不显示
- 100分啦!!!!C#如何将程序属性写进.ini文件,如何导入???
- c# 怎么实现BHO
- winform中dataGridView 点击删除按钮的事件时删除我选中的行
- 如何实现把客户端文件上传到服务器端指定位置?在线等待...
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("example.xml");
XmlNode root = xmlDoc.SelectSingleNode(节点名);//查找你需要的节点
foreach (XmlNode node in root)
{
...你挨个从数据库的纪录节点中取值就行
}
这是插入节点的操作
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("example.xml");
XmlNode root=xmlDoc.SelectSingleNode(节点名);//查找你要插入的节点
XmlElement xe1=xmlDoc.CreateElement(字节点名);
xe1.SetAttribute(名称,值);
xmlDoc.Save("example.xml");
通过SqlCommand.ExecuteXmlReader 方法返回XmlReader对象。拿到了XmlReader对象,楼主想怎么处理就怎么处理。而这种方式是效率最高而且是最省事也是最合理的。
关于SQL Server的Xml功能实例我写了两篇文章可以参考这里,其中一篇针对SQL Sql 2005的Xml字段。使用Microsoft SQL Server 2000的XML查询
http://blog.csdn.net/zhzuo/archive/2005/06/28/406290.aspx
使用SQL Server 2005 FOR XML嵌套查询
http://blog.csdn.net/zhzuo/archive/2008/06/08/2525447.aspx
private static void GetXmlDocumentDataFromSQL()
{
string s = "Data Source=127.0.0.1;Initial Catalog=AdventureWorks;Integrated Security=True";
SqlConnection conn = new SqlConnection(s);
string sql = "SELECT AdditionalContactInfo FROM Person.Contact WHERE ContactID = 1";
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
XmlReader reader = command.ExecuteXmlReader();
try
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.Save(@"c:\sqlxmldata.xml");
}
finally
{
reader.Close();
}
}
finally
{
conn.Close();
}
}////读取SQL 中 Xml类型字段保存到Xml文件(Xml字段包含Xml片段,即有多个根节点)
private static void GetXmlBlockDataFromSQL()
{
string s = "Data Source=127.0.0.1;Initial Catalog=AdventureWorks;Integrated Security=True";
SqlConnection conn = new SqlConnection(s);
string sql = "SELECT XmlEvent FROM DatabaseLog WHERE DatabaseLogID = 1;";
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
XmlReader reader = command.ExecuteXmlReader();
try
{
XmlDocument doc = new XmlDocument();
XmlElement rootNode = doc.CreateElement("Root");//创建一个根节点用来包含xml片段
doc.AppendChild(rootNode);
XmlNode node = doc.ReadNode(reader);
while (node != null)//循环处理xml片段
{
rootNode.AppendChild(node);
node = doc.ReadNode(reader);
}
doc.Save(@"c:\sqlxmldata1.xml");
}
finally
{
reader.Close();
}
}
finally
{
conn.Close();
}
}
能用while(Read())吗
是不是要先创建一个根节点
这个代码应该怎么写呀 谢谢!!
sqlstr = "select Document from dbo.Logs where EventID='" + EventID + "'";
SqlCommand command = new SqlCommand(sqlstr, Conn);
try
{
Conn.Open();
XmlReader reader = command.ExecuteXmlReader();
try
{
XmlDocument doc = new XmlDocument();
XmlElement rootNode = doc.CreateElement("Root");
doc.AppendChild(rootNode);
XmlNode node = doc.ReadNode(reader);
while (node != null)
{
rootNode.AppendChild(node);
node = doc.ReadNode(reader);
}
doc.Save(@"c:\combineXml.xml");如果我这样写的话 combimeXml.xml中只有第一个document字段的值,怎么把第二个document的值也加入到xml的root根节点下呀 谢谢!