开发者

bind cdata element to textbox c#

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>A开发者_C百科2</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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜