Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Xml Databinding - Why no result from XPath?

Closed Thread
 
Thread Tools Display Modes
Old 10-13-2006   #1 (permalink)
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?
Old 10-13-2006   #2 (permalink)
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?


Old 10-13-2006   #3 (permalink)
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?

>
>

Old 10-13-2006   #4 (permalink)
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?

Old 10-13-2006   #5 (permalink)
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?

>>
>>


Closed Thread

« Timer Tick | Urgent »
Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath of SelectNodes() does not work when tag contains xmlns Jazper .NET General 1 1 Week Ago 07:34 AM
XPath Query in Event Viewer Dave Lawlor Vista performance & maintenance 2 06-13-2008 08:21 AM
Xml Binding - Why does XPath return nothing? jmagaram Avalon 0 10-13-2006 04:25 PM
Xml Binding - Why does XPath return nothing? jmagaram Avalon 0 10-13-2006 04:24 PM
Binding, XPath & Namespaces Doug Avalon 4 01-10-2006 03:52 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50