tags:

views:

172

answers:

3

Hi:

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 is 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, Does 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 datagrideview or I have to use something like linq?

If I have to, Can someone show me how to do that using linq?

+2  A: 

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.

Roman Boiko
You can generate schema for your xml in Visual Studio (not in Express edition).
Roman Boiko
It is not necessary to add schema, see my code above.
Roman Boiko
Tested with attribute, also works.
Roman Boiko
Anyway, to bind data you need a simpler structure, like in your first example. Or you will not be able to use binding correctly. Or you'll need to write your own binding code.
Roman Boiko
Actually, it also works without passing `XmlReadMode.InferSchema` parameter. To sum up: I suppose the problem is that you can't bind your complex data (in your last example), and not that dataset can't read it. Try to stop in debugger and look at dataset's content - you'll see that it loaded data in 3 separate tables, which is clearly not what you intended to do.
Roman Boiko
A: 

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.

John Saunders
Wrong. They can, but not in general case.
Roman Boiko
Isn't that what I just said? The general case is "general-purpose ... XML". The general case does not work. What works is any XML that the DataSet could have generated. That does not include the case presented by the OP.
John Saunders
+1  A: 

Roman Boiko:

You can really read the attributes? I tried importing the examples above, but with attributes I can't get xml data show up in datagrid view. Please let me know how you did it.

Thanks

I would suggest you to set a breakpoint **after** you loaded data from xml file to DataSet, and watch its contents in DataSet Visualizer (http://msdn.microsoft.com/en-us/library/d480bk47(VS.80).aspx). You can play with different xml structures in your file and see how .net interprets importing. I also suggest you to look at `ReadXml` methods of DataSet class in Reflector - there are many interesting moments. Regarding binding - yes, default binding doesn't display attributes. Try to bind from `ds.Tables[1]` to some grid with auto-generated columns. Or implement your own binding.
Roman Boiko