0

I have an XML file, each element has name of a product and it's price.

I have a XAML file with a listbox.

How do I display all the items in the XML file in the listbox programmatically in C#? Thanks.

Here is my XML file with 3 sample products:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2011-09-20T15:04:12">
<Product>
<Name>Red Chair</Name>
<Price>29.5</Price>
</Product>
<Product>
<Name>Blue Chair</Name>
<Price>27</Price>
</Product>
<Product>
<Name>Round Table</Name>
<Price>31</Price>
</Product>
</dataroot>

Here's my XAML:

<Window x:Class="DockPanel.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Inventory" Height="350" Width="525">
 <DockPanel>

    <ListBox Name="listBox1" Margin="10" >


    </ListBox>

</DockPanel>

0

1 Answer 1

1

You want to use an XmlDataProvider (explained here). You'll need to out the provider in your Resources, speci path to get the data you want displayed, and then bind your ItemsSource to the resource

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

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.