把datatable里的数据转换成xml格式的字符串
string GetXmlByDataTable()
{
string xmlstr;
string sql = "select top 10 * from BasicInfo";
DataTable dt = idb.ReturnDataTable(sql);
dt.TableName = "tbname";
if (dt.Rows.Count>0)
{
System.IO.StringWriter writer = new System.IO.StringWriter();
dt.WriteXml(writer);
xmlstr = writer.ToString();
}
return xmlstr;
}
相关文档:
using System;
using System.Xml;
namespace ReadXMLfromFile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
......
最近在博客园代码的重构中, 我发觉用DataTable.Select进行XML数据的查询也是挺方便的。
比如我们根据Url中参数值查询XML数据中相应节点的数据。
假如我们有这样的Xml数据文件Catalog.xml:
<Navigation>
<Catalog title="非技术区" url="default.aspx?cate=2" rss="MainFeed.as ......
在 XML 中,一些字符拥有特殊的意义。
如果你把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始。
这样会产生 XML 错误:
<message>if salary < 1000 then</message>
为了避免这个错误,用一个实体引用来代替 "<" 字符:
<message>if salary < 1000 then ......
使用VS2005工具XSD.exe(SDK\v2.0\Bin\xsd.exe)自动生成实体类:
xsd /c /namespace:myCompany /language:CS temp1.xsd
也可以生成DataSet类型的类:
xsd /dataset /language:CS temp1.xsd
( 类文件和XSD之间可以相互转换,也就是说,你也可以先生成类,然后自动生成XSD)
自动读取XML数据 ......
Integration with the XML Data Type
With the introduction of the XML data type, we wanted to also give FOR XML the ability to generate an instance of XML directly (more precisely, it generates a single row, single column rowset where the cell contains the XML data type instance).
Because of the bac ......