![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| NAnt / <XMLPOKE> and xpath question ... | .NET General | |||
| XPath Query in Event Viewer | Vista performance & maintenance | |||