![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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? |
| | #2 (permalink) |
| 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? |
| | #3 (permalink) |
| 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? > > |
| | #4 (permalink) |
| 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? |
| | #5 (permalink) |
| 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? >> >> |
| |
| |
![]() |
| 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 |