1

I'm making my first steps with XML and binding data to DataGridView I have a XML file

<root>
    <setting id="1">
        <name1>val1</name1>
        <name2>val2</name2>
        <name3>val3</name3>
    </setting>
    <setting id= "2">
        <name1>val1</name1>
        <name2>val2</name2>
        <name3>val3</name3>
    </setting>
    <setting id= "3">

    </setting>
</root>

But what I get is

name1   name2   name3  
val1    val2    val3
val1    val2    val3
...

What I want is

name1  val1 
name2  val2  
name3  val3

what I did till now is

//...
DataGridView1.DataSource = PopulateDataGrid();
//...


    public DataTable PopulateDataGrid()
    {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(SettingsXMLPath);
        return dataSet.Tables[0];
    } 

when I run the code it makes rows for each block

what I'm I missing here ?

8
  • Define "rows for each block". What is happening that you didn't expect. What did you think was going to happen? Commented Aug 5, 2014 at 10:36
  • lets say I have two Settings Blocks within each of them I have 5 parameters --> I get to 3 rows ( name of params , id1 , id2 ) with 5 columns for each block and only 2 columns ( name , value ) and 5 rows for each <settings> Commented Aug 5, 2014 at 10:43
  • Take a look at XML Serialization. That should help you out. Commented Aug 5, 2014 at 10:46
  • @Matthijs can you help me out here ? I just cant rap my head around it Commented Aug 5, 2014 at 11:19
  • This doesn't really make sense in a logical thinking. Shouldn't all settings under one ID be under the same settings? So ID=1 has 3 values, ID = 2 has 3 values etc.. Commented Aug 5, 2014 at 12:02

1 Answer 1

1

Your XML file should like something like this to achieve the result you want.

<NewDataSet>
  <Table1>
    <Column1>Name1</Column1>
    <Column2>Value1</Column2>
  </Table1>
  <Table1>
    <Column1>Name2</Column1>
    <Column2>Value2</Column2>
  </Table1>
  <Table1>
    <Column1>Name3</Column1>
    <Column2>Value3</Column2>
  </Table1>
  <Table1>
    <Column1>Name4</Column1>
    <Column2>Value4</Column2>
  </Table1>
</NewDataSet>
Sign up to request clarification or add additional context in comments.

3 Comments

Is there is way to convert one form of XML to another ?
What do you mean by one form of XML to another? I don't know of any built-in conversion tool. I guess if you want to re-arrange the data you have to do it manually. The best way will be to fill a DataGridView1 with the original data and then transfer that data to another DataGridView2 in the format you want and then export it to an XML file.
yeah I thought it would be it ... thanks for clearing it up for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.