ADO.NET与XML的转换

1、读取XML文档到DataSet

读XML文档:从XML文件中载入一个DataSet的内容

string filePath = Server.MapPath("WebForm7.xml");
DataSet ds = new DataSet();
//使用文件名
ds.readxml(filePath); //readxml()将 XML 架构和数据读入 DataSet
//使用文件名
ds.readxmlSchema(filePath); //readxmlSchema()将 XML 架构读入 DataSet
//使用FileStream文件流
FileStream fs1 = new FileStream(filePath,FileMode.Open);
ds.readxmlSchema(fs1);
fs1.Close();
//使用StreamReader
StreamReader sr = new StreamReader(filePath);
ds.readxmlSchema(sr);
sr.Close();
//使用XmlTextReader
FileStream fs2 = new FileStream(filePath,FileMode.Open);
XmlTextReader xmlreader = new XmlTextReader(fs2);
ds.readxmlSchema(xmlreader);
xmlreader.Close();

2、DataSet转为XML文档

写XML文档:将DataSet的内容写入XML文件

//将 DataSet 的当前数据写入指定的文件
ds.WriteXml("d:\\test1.xml");
//将 XML 架构形式的 DataSet 结构写入文件
ds.WriteXmlSchema("d:\\test2.xml");
//返回存储在 DataSet 中的数据的 XML 表示形式字符串
string str = ds.GetXml();
//调用方法调用 WriteXml 并将 XmlWriteMode 设置为 IgnoreSchema 相同。
//返回存储在 DataSet 中的数据的 XML 表示形式的 XSD 架构字符串
string str2 = ds.GetXmlSchema();
//除了只写主要架构以外,调用方法调用 WriteXmlSchema 相同。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念