Jiminy Cricket
Jiminy Cricket

Reputation: 1377

How to bind XML to DataGrid - WPF

I have a DataGrid in WPF which I would like to populate with the results of an XML file, but I am unsure how to do so.

I have managed to populate the columns with various Element Attributes, but I would like to have separate columns for each Attribute which are assigned to Elements in my XML file.

My XML is formatted as follows:

<?xml version="1.0"?>
-<Roles User="Mr 1">
       <Role Info="Information 1" Name="Role1"> </Role>
       <Role Info="Information 2" Name="Role2"> </Role>
</Roles>

And so on... With various Users, with Roles assigned to each.

My VB.Net code is as follows:

Private Sub DataGridTest_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
 Dim xDoc = XDocument.Load("\Roles.xml")
 Dim xRoles = xDoc...<Role>

 MyDataGrid.DataContext = xRoles

End Sub

My XAML is as follows:

    <Grid Style="{StaticResource ContentRoot}">
           <DataGrid Name="MyDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Height="500" >
                <DataGrid.Columns>
                      <DataGridTextColumn Binding="{Binding XPath=@Name}" Header="Role" />
                      <DataGridTextColumn Binding="{Binding XPath=@Info }" Header="Information" />
                </DataGrid.Columns>
           </DataGrid>
    </Grid>

Whilst this DOES populate the DataGrid, it seems to populate it with all properties associated with xRole. I can understand this is happening, but I can't figure out how to get the outcome I'm looking for, which is: 'Info' and 'Name' as headers, and the Attribute Values as cells in their own columns.

I did try populating a list with the Attribute Values, manually creating the columns, then binding the list to the column - this may be the correct way to do it? - but I could only figure out how to bind 1 list to one of the columns. Is it possible to have 2 separate lists and bind them, individually, to their own columns?

I realize that I may be asking 2 separate questions here - apologies. I'm finding that information is sparse for specific issues - especially pertaining to VB.Net

Thanks in advance!

Upvotes: 1

Views: 2799

Answers (2)

dkozl
dkozl

Reputation: 33364

Since, according to MSDN

DataGrid cannot auto-generate columns when the source is XML data

you need to create columns manually

<DataGrid Name="MyDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Path=Elements[Role]}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Attribute[Name].Value}" Header="Name" />
        <DataGridTextColumn Binding="{Binding Path=Attribute[Info].Value}" Header="Info"/>
    </DataGrid.Columns>
</DataGrid>

and in code

MyDataGrid.DataContext = xDoc.Root

Upvotes: 1

waltmagic
waltmagic

Reputation: 642

You may have already tried this but you could use DataSet.ReadXml(filename).

    public DataSet getData()
    {
        DataSet ds;
        // Create an XmlReader
        using (XmlReader reader = XmlReader.Create(new StringReader(results)))
        {
            ds.ReadXml(reader);
        }

        return ds;
    }

I used a reader here but the same applies when you read directly from an xml file. After you have the DataSet then you can go through the tables like this...

  DataTable testDataTable1 = ds.Tables[0];
  DataTable testDataTable2 = ds.Tables[1];

you can loop through them to build a more custom data table. Hope this helps you in some way!

Upvotes: 0

Related Questions