Click here to Skip to main content
15,513,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a WPF application , where I need to bind the XML data to the ComboBox. Below code I have tried. But values are not not showing on the ComboBox. Anybody please helpme to solve this.

What I have tried:

This the Languages.xml File
<?xml version="1.0" encoding="utf-8" ?>
<Languages>
  <language name="English"/>
  <language name="Hindi"/>
  <language name="Odia"/>
  <language name="Telugu"/>
  <language name="Kannada"/>
</Languages>

And this is App.xaml
<Application x:Class="xmlBinding.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:xmlBinding"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <XmlDataProvider x:Key="XmlData" Source="Languages.xml"/>
    </Application.Resources>
</Application>


And this MainWindow.xaml
<grid>
<combobox x:name="cmbName" height="30" width="150" itemssource="{Binding Source={StaticResource XmlData},XPath=Languages/.}"
="" displaymemberpath="@name">
Posted
Updated 20-May-21 23:32pm
Comments
Chris Copeland 20-May-21 9:39am    
Have you checked in the Debug output window? Normally when there's an issue with XAML either loading or rendering a resource then it will print out a message accordingly.
Gita Padhihari 24-May-21 3:47am    
Hi,
Thank you for the solution. The issue got resolved.
Gerry Schmitz 20-May-21 13:19pm    
As a last resort, you can always create the "list" in code (hard-coded or deserialized xml). Doesn't look like that particular list will change much either.

1 solution

Did you have a look at the Debug Output Windows as suggested by Chris?

If you see a message like this
XmlDataProvider cannot load asynchronous document from Source because of load or parse error in XML stream. Uri:'Languages.xml' IOException:'System.IO.IOException: Cannot locate resource 'languages.xml'.
then the XML file cannot be found.

In this case you can try to assign a path like this:
<XmlDataProvider x:Key="XmlData" Source="c:\\Languages.xml" XPath="Languages"/>

Do not forget to copy the XML file to the specified path.
 
Share this answer
 
v2
Comments
Gita Padhihari 24-May-21 3:46am    
Hi,
Thanks for the solutions. Yes The issue got resolved.
TheRealSteveJudge 25-May-21 2:05am    
Thank you for your feedback!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900