在WPF/XAML中将textblock绑定到xml

问题描述:

这是我的xaml:

Here is my xaml:

<Window x:Class="WpfCanvasTest.Test"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="LearnWPF - XmlDataBinding" Height="300" Width="300"

    x:Name="Window"

    >
    <Window.Resources>
      <XmlDataProvider x:Key="EmployeeData" XPath="root/advertiser">
        <x:XData>
           <!--xml data island-->
          <root>
            <advertiser>
              <name>Pfizer Ltd</name>
              <address>Ramsgate Road, Sandwich</address>
              <postcode>CT13 9NJ</postcode>
              <adv_email />
              <fax />
              <tel>01304 616161</tel>
            </advertiser>
            <vacancy>
              <title>Product Manager</title>
              <refrence>62209</refrence>
              <contact />
              <location>UK</location>
              <type>Permanent</type>
              <advjobref>942042</advjobref>
              <email />
              <joburl></joburl>
              <jobdesc />
              <region>UK - All</region>
              <sector1>Medical Sales</sector1>
              <sector2 />
              <sector3 />
              <tel>01304 616161</tel>
            </vacancy>
          </root>
        </x:XData>
      </XmlDataProvider>
      <XmlDataProvider x:Key="LearnWPFFeed"

                       Source="http://learnwpf.com/Rss.ashx"

                       XPath="//item" />
    </Window.Resources>
    <StackPanel>
      <!-- binding to data island -->
      <StackPanel DataContext="{StaticResource EmployeeData}">
        <TextBlock Text="{Binding XPath=name}" FontSize="30" />
      </StackPanel>
    </StackPanel>
  </Window>




却什么也没显示:(
如果删除空缺元素及其子元素,则会显示该名称.

不知道怎么了
PS:这是我第一次尝试将textblock绑定到xml




But it''s not displaying anything :(
The name is displayed if I remove the vacancy element and its child elements.

Don''t know what is wrong
PS: This is my first time I am trying to binding textblock to xml



为了完成这项工作,我需要在根元素上提供一个空的命名空间:
<根 xmlns =" >

这是将XPath查询应用于XAML内联的数据岛的要求.

我希望这有助于您前进,
马丁
Hi There,

To make this work I needed to provide an empty namespace on the root element:
<root xmlns="">

This is a requirement for applying XPath queries to a data island that is inline within the XAML.

I hope this helps to get you going,
Martin