在 C# 中将 Xml 读入数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading Xml into a datagrid in C#
提问by Omar Kooheji
Whats the best way to read Xml from either an XmlDocument or a String into a DataGrid?
从 XmlDocument 或 String 读取 Xml 到 DataGrid 的最佳方法是什么?
Does the xml have to be in a particular format?
xml 是否必须采用特定格式?
Do I have to use A DataSet as an intermediary?
我是否必须使用数据集作为中介?
I'm working on a client that consumes Xml sent over from a Server which is being developed by one of my colleagues, I can get him to change the format of the Xml to match what a DataGrid requires.
我正在开发一个客户端,该客户端使用从我的一位同事正在开发的服务器发送的 Xml,我可以让他更改 Xml 的格式以匹配 DataGrid 的要求。
采纳答案by Omar Kooheji
We have a partial answer to get the data into a dataset but it reads it in as a set of tables with relational links.
我们有一个将数据放入数据集的部分答案,但它作为一组带有关系链接的表读取它。
DataSet ds = new DataSet();
XmlTextReader xmlreader = new XmlTextReader(xmlSource, XmlNodeType.Document, null);
ds.ReadXml(xmlreader);
回答by Rune Grimstad
It depends on which version of .NET you are running on. If you can use Linq2Xml then it is easy. Just create an XDocument and select the child nodes as a list of an anonymous type.
这取决于您运行的 .NET 版本。如果您可以使用 Linq2Xml,那就很容易了。只需创建一个 XDocument 并选择子节点作为匿名类型的列表。
If you can't use Linq2Xml then you have a few other options. Using a DataSet is one, this can work well, but it depends on the xml you are receiving. An other option is to create a class that describes the entity you will read from the xml and step through the xml nodes manually. A third option would be to use Xml serialization and deserialize the xml into a list of objects. This can work well as long as you have classes that are setup for it.
如果您不能使用 Linq2Xml,那么您还有其他一些选择。使用 DataSet 就是其中之一,这可以很好地工作,但这取决于您收到的 xml。另一种选择是创建一个类来描述您将从 xml 中读取的实体并手动单步执行 xml 节点。第三种选择是使用 Xml 序列化并将 xml 反序列化为对象列表。只要您有为它设置的类,它就可以很好地工作。
The easiest option will be either to create an XDocument or to create a DataSet as you suggest.
最简单的选择是按照您的建议创建 XDocument 或创建数据集。
回答by Roel Snetselaar
Obviously your XML needs to be valid :)
显然您的 XML 需要有效:)
After that, define a dataset, define a datagrid. Use the readXML method on the dataset to fill the dataset with your XML, finish with a dataBind and you are good to go.
之后,定义一个数据集,定义一个数据网格。在数据集上使用 readXML 方法用你的 XML 填充数据集,用 dataBind 完成,你就可以开始了。
DataSet myDataSet = new DataSet();
myDataSet .ReadXml(myXMLString);
myDataGrid.DataSource = myDataSet ;
myDataGrid.DataBind();
回答by darasd
You can simply use the XmlDatasource object as the grid's data source. That allows you to set the file and the XPath, in order to choose the XML that is the soure of your data. You can then use the <%# XPath="blah"%> function to write out your data explicitely, if you like.
您可以简单地使用 XmlDatasource 对象作为网格的数据源。这允许您设置文件和 XPath,以便选择作为数据源的 XML。然后,如果您愿意,您可以使用 <%# XPath="blah"%> 函数明确地写出您的数据。