views:

29

answers:

1

I am trying to bind a CData element to a text box, in code. the xml structure is like this:

<Music>
<Category>
<Albums>
 <![CDATA[
    <AlbumData>
      <Album>
          <id>A1</id>
          <Artist>artist 1</Artist>
      </Album>
      <Album>
          <id>A2</id>
          <Artist>artist 2</Artist>
      </Album>

    </AlbumData>
  ]]>
   </Albums>

  </Category>

 </Music>

How can I bind the Artist to a textbox? I want to make it a two-way binding.

A: 

To get the XML that has for some insane reason been embedded into a CDATA section by your customer, you can do the following:

XDocument xd = XDocument.Load("music.xml")
XElement albumData = XElement.Parse(xd.Root.Element("Category").Element("Albums").Value);

From there you can use the xml in an XmlDataProvider and use XPath to get to the artist's name and display it in a TextBox.

I think in your case (a desire to have two way access) it will be easier in the long run to create backing class (model and viewmodel) and then create them from the extracted XML. From those, instead of an XmlDataProvider, you set the DataContext of the window to the viewmodel.

Matt Ellen