0

In my WPF app the following should be possible:

reading in XML Data and presenting them in the User Interface.

To this end I defined a dataGrid in the MainPage.xaml file:

<DataGrid Name="dataGrid" AutoGenerateColumns="True" />

In the code-behind-file I created a method to fill this datagrid with data from an xml-file:

private void mnuOpen_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    Nullable<bool> result = dlg.ShowDialog();
    if (result == true)
     {
         string path = System.IO.Path.GetFullPath(dlg.FileName);
         var xml = XDocument.Load(path).Root;
         dataGrid.DataContext = xml;
     }
}

This compiles fine and also runs fine (whithout throwing an exception) - however, the xml-data is not displayed in the UI.

When using the debugger, there are several strange things:

  1. the variable "path" in the watch window looks as follows the whole time:path error CS0103: The name 'path' does not exist in the current context

  2. for some reason, the line

var xml = XDocument.Load(path).Root;

seems to be "jumped over". That is, the debugger goes directly from

string path = System.IO.Path.GetFullPath(dlg.FileName);

to

dataGrid.DataContext = xml;

I don't really understand what's going on here.

5
  • "Jumped over" doesn't make sense. What was the decision process that led to trying to populate a DataGrid by setting DataContext? Commented May 16, 2017 at 18:32
  • I used what was proposed under the following link: stackoverflow.com/questions/5238534/c-wpf-datagrid-and-xml-file ... see the section on "Code Behind" in the approved answer Commented May 16, 2017 at 18:34
  • Can you identify the instance of XmlDataProvider in your code? Commented May 16, 2017 at 18:34
  • I thought you use either XmlDataProvider or a "Code Behind" approach ... I don't have an instance of XMLDataProvider Commented May 16, 2017 at 18:38
  • You used a code-behind approach, so fortunately you're spared the horrors of trying to make XmlDataProvider work. Commented May 16, 2017 at 18:39

1 Answer 1

1

Since you're not using an XmlDataProvider, there's not a lot to be gained by using one or two fragments of that approach to the problem and leaving out the rest. XmlDataProvider is weird and anything involving it is going to be a workaround.

In WPF, you populate a DataGrid, or any "collection control" (ListBox, ComboBox, etc.) by setting or binding ItemsSource. That's the property that populates these things with a collection. The answer you looked at is just an ugly workaround to enable ItemsSource to be bound when XmlDataProvider is being used. DataContext is a much more general property that any WPF control has. It's the source that bindings use to find the properties you bind to. You're not binding anything, so worry about that later.

Since you're doing this in codebehind, this is quick and it works with the sample XML I've included at the bottom of this answer:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    Nullable<bool> result = dlg.ShowDialog();
    if (result == true)
    {
        string path = System.IO.Path.GetFullPath(dlg.FileName);

        DataSet dataSet = new DataSet();
        dataSet.ReadXml(path);
        DataView dataView = new DataView(dataSet.Tables[0]);
        dataGrid.ItemsSource = dataView;
    }
}

I'd probably either create POCO objects from the XML and expose those in a collection on a viewmodel, or if I were in a great hurry I might have the viewmodel expose a DataView as above. But if you haven't already gone MVVM with this project, you may as well leave that for the next one.

XML:

<Items>
  <Item Foo="0" Bar="A" />
  <Item Foo="1" Bar="B" />
  <Item Foo="2" Bar="C" />
  <Item Foo="3" Bar="D" />
</Items>

This works too:

<Items>
  <Item>
    <Foo>0</Foo>
    <Bar>A</Bar>
  </Item>
  <Item>
    <Foo>1</Foo>
    <Bar>B</Bar>
  </Item>
</Items>
Sign up to request clarification or add additional context in comments.

21 Comments

Now, using this code, I'm getting the following error message: "System.IndexOutOfRangeException occurred Message=Table 0 cannot be found."
@Tommy Can I see the exact code that's throwing that exception?
sure, I can send you the whole thing ... it's just about 100 lines of code in the MainPage.xaml.c file
No send, only post. I doubt that I need anything that doesn't touch the XML and/or the DataGrid. Questions aren't size limited, just stick four dashes in a row above the update and the markdown will create a horizontal bar.
You can forget about the first method (which is for text files and works fine) ... the method not working is: mnuOpen2_Click
|

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.