2

I have been trying to learn Visaul C#. Lately, I have been focusing on WPF. Here is the link to the tutorial I have been working on:

http://msdn.microsoft.com/en-us/library/vstudio/ms752299(v=vs.110).aspx

The application should let you view a person's expenses if you select their name and click the View button. Unfortunately, the expenses are not displayed. I keep getting this error message:

System.Windows.Data Error: 50 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").

I thought that xmlns needed to be left blank, so the app could navigate down the XAML. Here is the code for Grid resource that I made:

<Grid.Resources>
            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:XData>
                    <Expenses xmlns="">
                        <Person Name="Mike" Department="Legal">
                            <Expense ExpenseType="Lunch" ExpenseAmount="50" />
                            <Expense ExpenseType="Transportation" ExpenseAmount="50" />
                        </Person>
                        <Person Name="Lisa" Department="Marketing">
                            <Expense ExpenseType="Document Printing" ExpenseAmount="50" />
                            <Expense ExpenseType="Gift" ExpenseAmount="125" />
                        </Person>
                        <Person Name="John" Department="Engineering">
                            <Expense ExpenseType="Magazine Subscription" ExpenseAmount="50" />
                            <Expense ExpenseType="New Machine" ExpenseAmount="600" />
                            <Expense ExpenseType="Software" ExpenseAmount="500" />
                        </Person>
                        <Person Name="Mary" Department="Finance">
                            <Expense ExpenseType="Dinner" ExpenseAmount="100" />
                        </Person>
                    </Expenses>
                </x:XData>
            </XmlDataProvider>

            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>

</Grid.Resources>

Can anyone give me any hint about what is going wrong?

Thanks

1
  • You need to use an XmlNamespaceManager. From the book WPF 4.5 Unleashed "Whenever an XPath value has no prefix, the empty namespace is assumed to be the namespace URI. Therefore, even if your XML source has a default namespace, you must assign an XmlNamespaceManager for the queries to work". Commented Jun 11, 2014 at 23:05

2 Answers 2

1

Turns out the issue was not with the Grid resource! I forgot to set ItemsSource in DataGrid. In my ExpenseReportPage.xaml file, I typed the following:

<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
                  HorizontalAlignment="Left">
                <!-- Expense type and Amount table -->
                <DataGrid ItemsSource="{Binding XPath=Expense}" ColumnHeaderStyle="{StaticResource columnHeaderStyle}" 
                          AutoGenerateColumns="False" RowHeaderWidth="0">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Expense Type" Binding="{Binding XPath=@ExpenseType}" />
                        <DataGridTextColumn Header="Amount" Binding="{Binding XPath=@ExpenseAmount}"/>
                    </DataGrid.Columns>
                </DataGrid>
</Grid>

After ItemsSource was set, the data was displayed properly.

Sign up to request clarification or add additional context in comments.

Comments

0

In the book WPF 4.5 Unleashed he (Adam Nathan) gives a sample like so. xml:

<rss version = "2.0" xmlns:georss="http://www.gearss.org/georss">...</rss>

xaml:

<XmlDataProvider Source=http://api.twitter.com/1/statuses/user_timeline.rss?screen_name/adamnathan"
XmlNameSpaceManager="{StaticResource namespaceMapping}" XPath="rss/channel" x:Key="dataProvider" />

<XmllNamespaceMappingCollection x:Key="namespaceMapping">
    <XmlNamespaceMapping Uri="http://www.gearss.org/georss" Prefix="georss" />
</XmllNamespaceMappingCollection>

hth

Comments

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.