Can't import xml to dataset if xmlnode has attribute?
I am using the following codes to read xml file to a datagridview in c#:
newDataSet.ReadXml(filepath);
dataGridView3.DataSource = newDataSet;
dataGridView3.DataMember = "aaa";
And my xml file looks like this:
<root>
<aaa>
<Param_1>1</Param_1>
<Param_1>2</Param_1>
<Param_1>3</Param_1>
</aaa>
</root>
I can read the xml to dataset without any problems. Then I added some
attributes to the <Param>
nodes so it becomes
<Param_1 size="2">1</Param_1>
The dataset can't show any xml data, d开发者_如何学Pythonoes anyone knows why?
Also if I change my xml file to something like:
<root>
<Data_1>
<Name>aaa</Name>
<Params>
<Param_1>1</Param_1>
<Param_1>2</Param_1>
<Param_1>3</Param_1>
</Params>
</Data_1>
</root>
Is there still possible to use DataSet method to read them into a datagridview or I have to use something like linq?
If I have to, can someone show me how to do that using linq?
I suggest you to read data to xml document and bind to it using XmlDataSource, and not DataSet. And verify that structure is correct for binding. Looking at your comment (not at the edit by John, but rather at the comment under you question, there is no /
symbol which should be in the closing tag: </Data_1>
.
Or change structure to any that you wish, provided that it suits you for binding. After that you can read data:
DataSet ds = new DataSet();
ds.ReadXml("XMLFile1.xml", XmlReadMode.InferSchema);
Regarding Linq: you can start from reading Getting Started with LINQ in C#. But anyway you should not create complex xml structure - making it complex adds you a lot of work to deal with it.
DataSets are not a general-purpose mechanism for using XML. If the DataSet would not have produced the XML, then you cannot import that XML.
精彩评论