如何从WPF中的XML文件访问数据?

时间:2022-10-27 08:06:22

I am writing an rss feed with WPF and one of the feeds is a custom feed created by wordpress. It has some data that is nested within an <content:encoded> tag that I want to get at, but I have been unable to. I am trying to bind this data to some of my wpf controls.

我正在使用WPF编写RSS提要,其中一个提要是由wordpress创建的自定义提要。它有一些嵌套在我想要的 标签中的数据,但我无法做到。我试图将这些数据绑定到我的一些wpf控件。

some code:

<XmlDataProvider x:Key="" XPath="//item" Source="" />

then I have a listbox where the source is bound to this static resource, next I have a template that contains a number of controls that are bound to the xml data.

然后我有一个列表框,其中源绑定到此静态资源,接下来我有一个模板,其中包含许多绑定到xml数据的控件。

I can pull certain items from the xml file, i.e.

我可以从xml文件中提取某些项目,即

<TextBlock Text="{Binding XPath="title"} />

works okay. but now I want to access an <img src="....png"> that is nested inside <content:encoded>. XPath syntax like Source={Binding XPath="content-encoded/img/@src"} doesn't work.

工作还可以。但现在我想访问嵌套在 中的 如何从WPF中的XML文件访问数据?。像Source = {Binding XPath =“content-encoded / img / @src”}这样的XPath语法不起作用。

If you have any insight on how I can get at this part of the XML file it'd be great!

如果您对如何获取XML文件的这一部分有任何见解,那就太棒了!

1 个解决方案

#1


0  

content is an XML namespace that needs to be mapped, here's an example from some RSS-reader sample i wrote:

content是一个需要映射的XML命名空间,这是我写的一些RSS阅读器示例中的一个例子:

<Window.Resources>
    <s:Uri x:Key="FeedUri">
        http://backend.deviantart.com/rss.xml?q=boost%3Apopular+max_age%3A8h&amp;type=deviation
    </s:Uri>
    <XmlNamespaceMappingCollection x:Key="NsMapping">
        <XmlNamespaceMapping Prefix="media" Uri="http://search.yahoo.com/mrss/"/> <!-- Slash at the end on dA for some reason... -->
        <XmlNamespaceMapping Prefix="atom" Uri="http://www.w3.org/2005/Atom"/>
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Key="Data" Source="{StaticResource FeedUri}"
                     XmlNamespaceManager="{StaticResource NsMapping}"/>
</Window.Resources>

<!-- media and atom being used in a binding: -->
<Image Source="{Binding XPath=media:thumbnail/@url}" ToolTip="{Binding XPath=title}"/>
<Image Source="{Binding XPath=/rss/channel/atom:icon}" .../>

So with your mapping for content in place it should be: {Binding XPath="content:encoded/img/@src"}

因此,对于内容的映射,它应该是:{Binding XPath =“content:encoded / img / @src”}

#1


0  

content is an XML namespace that needs to be mapped, here's an example from some RSS-reader sample i wrote:

content是一个需要映射的XML命名空间,这是我写的一些RSS阅读器示例中的一个例子:

<Window.Resources>
    <s:Uri x:Key="FeedUri">
        http://backend.deviantart.com/rss.xml?q=boost%3Apopular+max_age%3A8h&amp;type=deviation
    </s:Uri>
    <XmlNamespaceMappingCollection x:Key="NsMapping">
        <XmlNamespaceMapping Prefix="media" Uri="http://search.yahoo.com/mrss/"/> <!-- Slash at the end on dA for some reason... -->
        <XmlNamespaceMapping Prefix="atom" Uri="http://www.w3.org/2005/Atom"/>
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Key="Data" Source="{StaticResource FeedUri}"
                     XmlNamespaceManager="{StaticResource NsMapping}"/>
</Window.Resources>

<!-- media and atom being used in a binding: -->
<Image Source="{Binding XPath=media:thumbnail/@url}" ToolTip="{Binding XPath=title}"/>
<Image Source="{Binding XPath=/rss/channel/atom:icon}" .../>

So with your mapping for content in place it should be: {Binding XPath="content:encoded/img/@src"}

因此,对于内容的映射,它应该是:{Binding XPath =“content:encoded / img / @src”}