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?