Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Xml Binding - Why does XPath return nothing?

 
 
Old 10-13-2006   #1 (permalink)
jmagaram


 
 

Xml Binding - Why does XPath return nothing?

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
 

Thread Tools



Vista Forums 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 Ltd

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