如何将XML读入数据集 [英] How to Read XML into a DataSet
问题描述
我有一个类到一个URL,并使用xmlDoc.Load(URL)获取一个xml文档。为了测试类,我添加了一个Web项目,以在网格视图中显示xml。
I have a class that goes to a URL and gets a xml document using xmlDoc.Load(URL). To test the class, I added a web project to display the xml in a grid view.
在按钮中,单击我创建一个xml文档的实例,并将其填充为:
In a button click I create an instance of an xml document and populate it as:
xmlDoc = myClassName()
xmlDoc = myClassName()
我被困在如何将xmlDoc转换为可使用的格式数据源
I'm stuck at how to get xmlDoc into a format usable by the datasource
我完全不知道如何让xml在grid中显示为dataset.ReadXml似乎需要一个文件路径。我不明白其他的重载。我想我必须将xml读入一个字符串或其他东西,但我不明白如何做 - 即使在阅读了大量的帖子和MSDN - 谢谢!
I am totally confused as to how to get the xml to be displayed in the grid as dataset.ReadXml seems to want a file path. I don't understand the other overloads. I suppose I have to read the xml into a string or something else, but I don't understand how to do this - even after reading numerous posts here and MSDN - Thanks!
推荐答案
示例:
string xml =@"<xml><customer><id>1</id></customer></xml>";
DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));
现在将数据源设置为您的网格:
Now set the datasource to your grid:
grid.DataSource=newDataSet.Tables[0];
更新:
DataSet ds = new DataSet();
//xmlDocument is your XmlDocument instance
ds.ReadXml(XmlReader.Create(new StringReader(xmlDocument.InnerXml)));
grid.DataSource=newDataSet.Tables[0];
这篇关于如何将XML读入数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!