Reputation: 2927
I have below xml file. I have copied it into my project debug/bin folder & have also attached to my project.
<?xml version="1.0" standalone="yes" ?>
- <NorthwindDataSet xmlns="http://tempuri.org/NorthwindDataSet.xsd">
- <Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
- <Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitución 2222</Address>
<City>México D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
I want to bind properties like CustomerName, City in my WPF application. I tried to bind it in XAML as below, but not getting success. Need suggetsion, what I am doing wrong.
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
Upvotes: 0
Views: 363
Reputation: 1782
Using your code I modified the XPath a little bit, and removed the reference to the XSD which you didn't provide, and it worked:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="NorthData" XPath="NorthwindDataSet/Customers">
<x:XData>
<NorthwindDataSet xmlns="">
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitucion 2222</Address>
<City>Mexico D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address
, FallbackValue=BindingFailed
, Source={StaticResource NorthData}}"
Height="28"
HorizontalAlignment="Left"
Margin="118,94,0,0" Name="label1"
VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData}
, XPath=City
, FallbackValue=BindingFailed}"
Height="100"
HorizontalAlignment="Left"
Margin="128,144,0,0"
Name="listBox1"
VerticalAlignment="Top"
Width="120" />
</Grid>
</Window>
Upvotes: 1
Reputation: 2204
You have to set the DataContext to your XML
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid DataContext="{StaticResource NorthData}">
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
Upvotes: 0