如何获取SQL Server数据库的XML表示形式? [英] How to get XML representation of SQL server database ?

查看:88
本文介绍了如何获取SQL Server数据库的XML表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取SQL Server数据库的XML表示形式?
例如,用户将输入www.blablabla.com?name=joe
结果是基于数据库的XML.例如:

<USER>
<ADDRESS>aaa</ADDRESS>
<PHONE>7873875</PHONE>
<AGE>67</AGE>
</USER>



我不希望结果像控件一样显示在xml GUI中,我希望它像这样纯XML

<USER>
<ADDRESS>aaa</ADDRESS>
<PHONE>7873875</PHONE>
<AGE>67</AGE>
</USER>



感谢adanced

解决方案

从此处开始: http ://msdn.microsoft.com/en-us/library/ms178107.aspx [ ^ ]


您可以使用XQuery进行xml操作,

检查网址,
http://www.simple-talk.com /sql/learn-sql-server/the-xml-methods-in-sql-server/ [ StringWriter strWriter = new StringWriter(); XmlTextWriter xmlWriter =新的XmlTextWriter(strWriter); xmlWriter.WriteStartElement("RootNode"); //您可以在其中具有所需的表名,而不是RootNode. SqlConnection mySqlConnection; mySqlConnection =新的SqlConnection(sConnection); mySqlConnection.Open(); XmlReader xmlReader = new SqlCommand("Select * From Table1 FOR XML AUTO,Elements",mySqlConnection).ExecuteXmlReader(); xmlWriter.WriteNode(xmlReader,true); xmlReader.Close(); xmlWriter.WriteEndElement(); mySqlConnection.Close();



您将在strWriter中拥有xml字符串.您可以使用XmlSerializer class.

或,

您可以从以下链接获得帮助
http://blogs. msdn.com/b/saurabh_singh/archive/2010/05/11/export-sql-table-records-to-xml-form.aspx [ 解决方案

Start here : http://msdn.microsoft.com/en-us/library/ms178107.aspx[^]


You can use XQuery for xml operation,

Check the url,
http://www.simple-talk.com/sql/learn-sql-server/the-xml-methods-in-sql-server/[^]

cheers


You can use XmlSerialization.

StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);

xmlWriter.WriteStartElement("RootNode"); 
// instead of RootNode, you can have the required table name in it.

SqlConnection mySqlConnection;
mySqlConnection = new SqlConnection(sConnection);
mySqlConnection.Open();

XmlReader xmlReader = new SqlCommand("Select * From Table1 FOR XML AUTO, Elements", mySqlConnection).ExecuteXmlReader();
xmlWriter.WriteNode(xmlReader, true);

xmlReader.Close();
xmlWriter.WriteEndElement(); 

mySqlConnection.Close();



You will have the xml string in strWriter. You can use XmlSerializer class.

OR,

You can get help from the below link
http://blogs.msdn.com/b/saurabh_singh/archive/2010/05/11/export-sql-table-records-to-xml-form.aspx[^]


这篇关于如何获取SQL Server数据库的XML表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆