locked
Accessing XML from code (Expression Blend 3, WPF) RRS feed

  • Question

  • Hi,
        I'm new to data in wpf and winforms and can't seem to find any way of accessing XML data which I have added to my project as a file and data source.
        is there a way of doing this, or a link to an appropriate tutorial?

    thanks,

         Admin jr
    Languages: Visual Basic, C, C++, Python,HTML, XML, C#, JavaScript, XAML
    • Edited by Admin Jr Thursday, May 28, 2009 8:01 PM
    Thursday, May 28, 2009 7:59 PM

Answers

  • In WPF you can use your XML-datasource by using the XMLDataProvider.
     You have to set the XML-File and the XPath-Element

    <XmlDataProvider x:Key="productsProvider" Source="store.xml"

     

    XPath="/Products"></XmlDataProvider>

    <Products>
    <Product>
    <ProductID>355</ProductID>
    <CategoryID>16</CategoryID>
    <ModelNumber>RU007</ModelNumber>
    <ModelName>Rain Racer 2000</ModelName>
    <ProductImage>image.gif</ProductImage>
    <UnitCost>1499.99</UnitCost>
    <Description>Looks like an ordinary bumbershoot ... </Description>
    </Product>
    <Product>
    <ProductID>356</ProductID>
    <CategoryID>20</CategoryID>
    <ModelNumber>STKY1</ModelNumber>
    <ModelName>Edible Tape</ModelName>
    <ProductImage>image.gif</ProductImage>
    <UnitCost>3.99</UnitCost>
    <Description>The latest in personal survival gear ... </Description>
    </Product>
    ...
    </Products>
    If you bind the list to an ListBox you have than to set the ItemsSource to something like

    ItemsSource="{Binding Source={StaticResource productsProvider},

     

    XPath=Product}"

    • Marked as answer by Admin Jr Sunday, May 31, 2009 2:50 PM
    Friday, May 29, 2009 5:25 AM

All replies

  • In WPF you can use your XML-datasource by using the XMLDataProvider.
     You have to set the XML-File and the XPath-Element

    <XmlDataProvider x:Key="productsProvider" Source="store.xml"

     

    XPath="/Products"></XmlDataProvider>

    <Products>
    <Product>
    <ProductID>355</ProductID>
    <CategoryID>16</CategoryID>
    <ModelNumber>RU007</ModelNumber>
    <ModelName>Rain Racer 2000</ModelName>
    <ProductImage>image.gif</ProductImage>
    <UnitCost>1499.99</UnitCost>
    <Description>Looks like an ordinary bumbershoot ... </Description>
    </Product>
    <Product>
    <ProductID>356</ProductID>
    <CategoryID>20</CategoryID>
    <ModelNumber>STKY1</ModelNumber>
    <ModelName>Edible Tape</ModelName>
    <ProductImage>image.gif</ProductImage>
    <UnitCost>3.99</UnitCost>
    <Description>The latest in personal survival gear ... </Description>
    </Product>
    ...
    </Products>
    If you bind the list to an ListBox you have than to set the ItemsSource to something like

    ItemsSource="{Binding Source={StaticResource productsProvider},

     

    XPath=Product}"

    • Marked as answer by Admin Jr Sunday, May 31, 2009 2:50 PM
    Friday, May 29, 2009 5:25 AM
  • Thanks a lot. But now I have another question. How can you then retrieve the values of the nodes from code?
    Languages: Visual Basic, C, C++, Python,HTML, XML, C#, JavaScript, XAML
    Sunday, May 31, 2009 3:45 PM
  • Here you have to create your own XML-Parser and select the nodes with xpath-expressions.
    Tuesday, June 2, 2009 9:51 AM