Windows Vista Forums

Xml Databinding - Why no result from XPath?
  1. #1


    jmagaram Guest

    Xml Databinding - Why no result from XPath?

    I am trying to bind to an XmlElement but it isn't working. Why isn't the
    third binding returning any text? These are my Xaml fragments:

    <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    <TextBlock Text="{Binding XPath=*}"></TextBlock>
    <TextBlock Text="{Binding XPath=FirstName}"></TextBlock>

    This is the text returned from the first binding:

    <Person xmlns="jdata">
    <PersonID>4</PersonID>
    <FirstName>Justin</FirstName>
    <LastName>Magaram</LastName>
    </Person>

    The second binding returns '4' which I think is correct. But the third
    binding returns nothing. I've tried different flavors:

    ../FirstName
    jdata:FirstName

    The XmlDataProvider is created in code. There is no reference to the
    XmlDataProvider in the markup. The Xml looks like this:



    <Sample xmlns="jdata">
    <Person>...
    <Person>...

    I load it into a DataSet:

    sampleDataSet.ReadXml(@"Sample.xml");

    I then create an XmlDataDocument:

    xmlDoc = new XmlDataDocument(sampleDataSet);

    To do any XPath against xmlDoc, it seems I need to create a namespace manager:

    namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    namespaceMgr.AddNamespace("default", sampleDataSet.Namespace);

    I then create my XmlDataProvider:

    XmlDataProvider xp = new XmlDataProvider();
    xp.Document = f.XmlDoc;
    xp.XmlNamespaceManager = f.NamespaceManager;
    xp.XPath = "/";

    Any here I assign my listbox to the data:

    XmlNodeList nodeList =
    xp.Document.SelectNodes(@"/default:Sample/default:Person",namespaceMgr);
    listbox1.DataContext = xp;
    listbox1.ItemsSource = nodeList;

    By the way, it is really annoying to have to preface all elements in the
    XPath with 'default:'. Is there any way to avoid this?

      My System SpecsSystem Spec

  2. #2


    Douglas Stockwell Guest

    Re: Xml Databinding - Why no result from XPath?

    I think it's the xml namespace (jdata) causing your problem.

    You need to use the XmlNamespaceManager attached property of the
    System.Windows.Data.Binding class.

    - Doug

    "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    news:F7B2DD48-66F0-42AC-A5FD-AA5292B77DD8@microsoft.com...
    >I am trying to bind to an XmlElement but it isn't working. Why isn't the
    > third binding returning any text? These are my Xaml fragments:
    >
    > <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    > <TextBlock Text="{Binding XPath=*}"></TextBlock>
    > <TextBlock Text="{Binding XPath=FirstName}"></TextBlock>
    >
    > This is the text returned from the first binding:
    >
    > <Person xmlns="jdata">
    > <PersonID>4</PersonID>
    > <FirstName>Justin</FirstName>
    > <LastName>Magaram</LastName>
    > </Person>
    >
    > The second binding returns '4' which I think is correct. But the third
    > binding returns nothing. I've tried different flavors:
    >
    > ./FirstName
    > jdata:FirstName
    >
    > The XmlDataProvider is created in code. There is no reference to the
    > XmlDataProvider in the markup. The Xml looks like this:
    >
    > <Sample xmlns="jdata">
    > <Person>...
    > <Person>...
    >
    > I load it into a DataSet:
    >
    > sampleDataSet.ReadXml(@"Sample.xml");
    >
    > I then create an XmlDataDocument:
    >
    > xmlDoc = new XmlDataDocument(sampleDataSet);
    >
    > To do any XPath against xmlDoc, it seems I need to create a namespace
    > manager:
    >
    > namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    > namespaceMgr.AddNamespace("default", sampleDataSet.Namespace);
    >
    > I then create my XmlDataProvider:
    >
    > XmlDataProvider xp = new XmlDataProvider();
    > xp.Document = f.XmlDoc;
    > xp.XmlNamespaceManager = f.NamespaceManager;
    > xp.XPath = "/";
    >
    > Any here I assign my listbox to the data:
    >
    > XmlNodeList nodeList =
    > xp.Document.SelectNodes(@"/default:Sample/default:Person",namespaceMgr);
    > listbox1.DataContext = xp;
    > listbox1.ItemsSource = nodeList;
    >
    > By the way, it is really annoying to have to preface all elements in the
    > XPath with 'default:'. Is there any way to avoid this?



      My System SpecsSystem Spec

  3. #3


    jmagaram Guest

    Re: Xml Databinding - Why no result from XPath?

    I've got one created. What do I attach it to? Do I need to manually
    instantiate an XmlDataProvider?

    "Douglas Stockwell" wrote:

    > I think it's the xml namespace (jdata) causing your problem.
    >
    > You need to use the XmlNamespaceManager attached property of the
    > System.Windows.Data.Binding class.
    >
    > - Doug
    >
    > "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    > news:F7B2DD48-66F0-42AC-A5FD-AA5292B77DD8@microsoft.com...
    > >I am trying to bind to an XmlElement but it isn't working. Why isn't the
    > > third binding returning any text? These are my Xaml fragments:
    > >
    > > <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    > > <TextBlock Text="{Binding XPath=*}"></TextBlock>
    > > <TextBlock Text="{Binding XPath=FirstName}"></TextBlock>
    > >
    > > This is the text returned from the first binding:
    > >
    > > <Person xmlns="jdata">
    > > <PersonID>4</PersonID>
    > > <FirstName>Justin</FirstName>
    > > <LastName>Magaram</LastName>
    > > </Person>
    > >
    > > The second binding returns '4' which I think is correct. But the third
    > > binding returns nothing. I've tried different flavors:
    > >
    > > ./FirstName
    > > jdata:FirstName
    > >
    > > The XmlDataProvider is created in code. There is no reference to the
    > > XmlDataProvider in the markup. The Xml looks like this:
    > >
    > > <Sample xmlns="jdata">
    > > <Person>...
    > > <Person>...
    > >
    > > I load it into a DataSet:
    > >
    > > sampleDataSet.ReadXml(@"Sample.xml");
    > >
    > > I then create an XmlDataDocument:
    > >
    > > xmlDoc = new XmlDataDocument(sampleDataSet);
    > >
    > > To do any XPath against xmlDoc, it seems I need to create a namespace
    > > manager:
    > >
    > > namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    > > namespaceMgr.AddNamespace("default", sampleDataSet.Namespace);
    > >
    > > I then create my XmlDataProvider:
    > >
    > > XmlDataProvider xp = new XmlDataProvider();
    > > xp.Document = f.XmlDoc;
    > > xp.XmlNamespaceManager = f.NamespaceManager;
    > > xp.XPath = "/";
    > >
    > > Any here I assign my listbox to the data:
    > >
    > > XmlNodeList nodeList =
    > > xp.Document.SelectNodes(@"/default:Sample/default:Person",namespaceMgr);
    > > listbox1.DataContext = xp;
    > > listbox1.ItemsSource = nodeList;
    > >
    > > By the way, it is really annoying to have to preface all elements in the
    > > XPath with 'default:'. Is there any way to avoid this?

    >
    >


      My System SpecsSystem Spec

  4. #4


    jmagaram Guest

    RE: Xml Databinding - Why no result from XPath?

    I've tried manually creating a Binding object and setting ItemsSource to it,
    but can't seem to get it all to work. The samples don't show how to bind to a
    dynamically loaded Xml file without an XmlDataProvider in the Xaml.

    "jmagaram" wrote:

    > I am trying to bind to an XmlElement but it isn't working. Why isn't the
    > third binding returning any text? These are my Xaml fragments:
    >
    > <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    > <TextBlock Text="{Binding XPath=*}"></TextBlock>
    > <TextBlock Text="{Binding XPath=FirstName}"></TextBlock>
    >
    > This is the text returned from the first binding:
    >
    > <Person xmlns="jdata">
    > <PersonID>4</PersonID>
    > <FirstName>Justin</FirstName>
    > <LastName>Magaram</LastName>
    > </Person>
    >
    > The second binding returns '4' which I think is correct. But the third
    > binding returns nothing. I've tried different flavors:
    >
    > ./FirstName
    > jdata:FirstName
    >
    > The XmlDataProvider is created in code. There is no reference to the
    > XmlDataProvider in the markup. The Xml looks like this:
    >
    > <Sample xmlns="jdata">
    > <Person>...
    > <Person>...
    >
    > I load it into a DataSet:
    >
    > sampleDataSet.ReadXml(@"Sample.xml");
    >
    > I then create an XmlDataDocument:
    >
    > xmlDoc = new XmlDataDocument(sampleDataSet);
    >
    > To do any XPath against xmlDoc, it seems I need to create a namespace manager:
    >
    > namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    > namespaceMgr.AddNamespace("default", sampleDataSet.Namespace);
    >
    > I then create my XmlDataProvider:
    >
    > XmlDataProvider xp = new XmlDataProvider();
    > xp.Document = f.XmlDoc;
    > xp.XmlNamespaceManager = f.NamespaceManager;
    > xp.XPath = "/";
    >
    > Any here I assign my listbox to the data:
    >
    > XmlNodeList nodeList =
    > xp.Document.SelectNodes(@"/default:Sample/default:Person",namespaceMgr);
    > listbox1.DataContext = xp;
    > listbox1.ItemsSource = nodeList;
    >
    > By the way, it is really annoying to have to preface all elements in the
    > XPath with 'default:'. Is there any way to avoid this?


      My System SpecsSystem Spec

  5. #5


    Douglas Stockwell Guest

    Re: Xml Databinding - Why no result from XPath?

    Here's a sample that runs in XamlPad.

    You can drop the XmlNamespaceManager at any point in the tree and it will
    cascade into any bindings below. In this example I've just dropped it on the
    single TextBlock you were having trouble with.

    I've manually created your XML as an island in an XmlDataProvider, but
    pulling an ordinary XmlDocument into the DataContext should also work.

    - Doug

    <StackPanel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel.Resources>
    <XmlNamespaceMappingCollection x:Key="namespace">
    <XmlNamespaceMapping Prefix="jdata" Uri="jdata" />
    </XmlNamespaceMappingCollection>
    </StackPanel.Resources>
    <StackPanel.DataContext>
    <XmlDataProvider>
    <x:XData>
    <Person xmlns="jdata">
    <PersonID>4</PersonID>
    <FirstName>Justin</FirstName>
    <LastName>Magaram</LastName>
    </Person>
    </x:XData>
    </XmlDataProvider>
    </StackPanel.DataContext>
    <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    <TextBlock Text="{Binding XPath=*}"></TextBlock>
    <TextBlock Binding.XmlNamespaceManager="{StaticResource namespace}"
    Text="{Binding XPath=/jdata:Person/jdata:FirstName}"></TextBlock>
    </StackPanel>


    "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    news:4BEE564E-191C-42F2-8A0C-8CBBE7ACCDC1@microsoft.com...
    > I've got one created. What do I attach it to? Do I need to manually
    > instantiate an XmlDataProvider?
    >
    > "Douglas Stockwell" wrote:
    >
    >> I think it's the xml namespace (jdata) causing your problem.
    >>
    >> You need to use the XmlNamespaceManager attached property of the
    >> System.Windows.Data.Binding class.
    >>
    >> - Doug
    >>
    >> "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    >> news:F7B2DD48-66F0-42AC-A5FD-AA5292B77DD8@microsoft.com...
    >> >I am trying to bind to an XmlElement but it isn't working. Why isn't the
    >> > third binding returning any text? These are my Xaml fragments:
    >> >
    >> > <TextBlock Text="{Binding Path=OuterXml}"></TextBlock>
    >> > <TextBlock Text="{Binding XPath=*}"></TextBlock>
    >> > <TextBlock Text="{Binding XPath=FirstName}"></TextBlock>
    >> >
    >> > This is the text returned from the first binding:
    >> >
    >> > <Person xmlns="jdata">
    >> > <PersonID>4</PersonID>
    >> > <FirstName>Justin</FirstName>
    >> > <LastName>Magaram</LastName>
    >> > </Person>
    >> >
    >> > The second binding returns '4' which I think is correct. But the third
    >> > binding returns nothing. I've tried different flavors:
    >> >
    >> > ./FirstName
    >> > jdata:FirstName
    >> >
    >> > The XmlDataProvider is created in code. There is no reference to the
    >> > XmlDataProvider in the markup. The Xml looks like this:
    >> >
    >> > <Sample xmlns="jdata">
    >> > <Person>...
    >> > <Person>...
    >> >
    >> > I load it into a DataSet:
    >> >
    >> > sampleDataSet.ReadXml(@"Sample.xml");
    >> >
    >> > I then create an XmlDataDocument:
    >> >
    >> > xmlDoc = new XmlDataDocument(sampleDataSet);
    >> >
    >> > To do any XPath against xmlDoc, it seems I need to create a namespace
    >> > manager:
    >> >
    >> > namespaceMgr = new XmlNamespaceManager(xmlDoc.NameTable);
    >> > namespaceMgr.AddNamespace("default", sampleDataSet.Namespace);
    >> >
    >> > I then create my XmlDataProvider:
    >> >
    >> > XmlDataProvider xp = new XmlDataProvider();
    >> > xp.Document = f.XmlDoc;
    >> > xp.XmlNamespaceManager = f.NamespaceManager;
    >> > xp.XPath = "/";
    >> >
    >> > Any here I assign my listbox to the data:
    >> >
    >> > XmlNodeList nodeList =
    >> > xp.Document.SelectNodes(@"/default:Sample/default:Person",namespaceMgr);
    >> > listbox1.DataContext = xp;
    >> > listbox1.ItemsSource = nodeList;
    >> >
    >> > By the way, it is really annoying to have to preface all elements in
    >> > the
    >> > XPath with 'default:'. Is there any way to avoid this?

    >>
    >>



      My System SpecsSystem Spec

Xml Databinding - Why no result from XPath? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath of SelectNodes() does not work when tag contains xmlns Jazper .NET General 1 27 Aug 2008
XPath Query in Event Viewer Dave Lawlor Vista performance & maintenance 2 13 Jun 2008
Xml Binding - Why does XPath return nothing? jmagaram Avalon 0 13 Oct 2006
Xml Binding - Why does XPath return nothing? jmagaram Avalon 0 13 Oct 2006
Binding, XPath & Namespaces Doug Avalon 1 10 Jan 2006