Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Need help binding XmlDataProvider as a source...

$
0
0

Hi,

Technology: Prism, Mef, Mvvm, C#

Read a quite a few articles and examples on the internet and I'm still having trouble connecting my.xml data to either a TreeView or ListBox via XmlDataProvider.  I need the XmlDataProvider because my.xml data needs the XmlNamespaceManager to resolve namespace notation.

This file is extracted from a .ods LibraOffice Calc Spreadsheet and I want to read directly from the file.

My .xml file:

<?xml version="1.0" encoding="utf-8"?><office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:grddl="http://www.w3.org/2003/g/data-view#" office:version="1.2"><office:meta><meta:initial-creator>nasheayahu</meta:initial-creator><dc:creator>Nasheayahu Miller</dc:creator><meta:print-date>2012-10-13T15:26:07</meta:print-date><meta:creation-date>2012-10-03T21:01:08</meta:creation-date><dc:date>2013-03-07T22:27:01.15</dc:date><meta:generator>LibreOffice/4.0.0.3$Windows_x86 LibreOffice_project/7545bee9c2a0782548772a21bc84a9dcc583b89</meta:generator><meta:editing-duration>PT20S</meta:editing-duration><meta:editing-cycles>2</meta:editing-cycles><meta:document-statistic meta:table-count="2" meta:cell-count="309343" meta:object-count="0" /><meta:user-defined meta:name="AppVersion">14.0300</meta:user-defined><meta:user-defined meta:name="DocSecurity" meta:value-type="float">0</meta:user-defined><meta:user-defined meta:name="HyperlinksChanged" meta:value-type="boolean">false</meta:user-defined><meta:user-defined meta:name="LinksUpToDate" meta:value-type="boolean">false</meta:user-defined><meta:user-defined meta:name="ScaleCrop" meta:value-type="boolean">false</meta:user-defined><meta:user-defined meta:name="ShareDoc" meta:value-type="boolean">false</meta:user-defined></office:meta></office:document-meta>

My View:

<UserControl 
    x:Class="StockTraderRI.Modules.Meta.MetaList.MetaListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Converters="clr-namespace:StockTraderRI.Infrastructure.Converters;assembly=StockTraderRI.Infrastructure"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="350" d:DesignWidth="720"><UserControl.Resources><DataTemplate x:Key="metaItemTemplate"><StackPanel Orientation="Horizontal"><Label Content="{Binding XPath=meta:user-defined}"/></StackPanel></DataTemplate></UserControl.Resources><Grid x:Name="LayoutRoot"
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><Grid x:Name="LayoutContent" Grid.Row="1"
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"><StackPanel DataContext="{Binding OdsXmlDataProvider}" ><ListBox  
                    ItemsSource="{Binding}"
                    ItemTemplate="{StaticResource metaItemTemplate}"
                    IsSynchronizedWithCurrentItem="True"
                    Visibility="Visible" SelectionMode="Single"/><TextBox Text="{Binding XPath=meta:name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /></StackPanel><StackPanel Grid.Column="1"
                    DataContext="{Binding OdsXmlDataProvider}"><TreeView 
                ItemsSource="{Binding}"></TreeView></StackPanel></Grid></Grid></UserControl>

Logic to create the connection:

        private XmlNamespaceManager LoadXmlNamespaceManager()
        {
            if (this.nsmanager == null)
            {
                this.nsmanager = new XmlNamespaceManager(xmDocumentFile.NameTable);
                if (xmDocumentFile.DocumentElement.HasAttributes)
                {
                    foreach (XmlAttribute attr in xmDocumentFile.DocumentElement.Attributes)
                    {
                        if (attr.Prefix.ToLower().Equals("xmlns"))
                        {
                            string url = nsmanager.LookupPrefix(attr.Value);
                            if (string.IsNullOrEmpty(url))
                            {
                                nsmanager.AddNamespace(attr.LocalName, attr.Value);
                            }
                        }
                    }
                }
            }

            return this.nsmanager;
        }

        private XmlDataProvider odsXmlDataProvider;
        public XmlDataProvider OdsXmlDataProvider
        {
            get
            {
                if (this.xmDocumentFile == null)
                {
                    string xmlFile = System.IO.Path.Combine(
                        this.propertiesService.DataXmlDirectory,
                        this.odsDocumentSectionService.OdsGetXmlFile(ModuleNames.MetaModule));
                    if (!File.Exists(xmlFile))
                        return null;

                    this.xmDocumentFile = new XmlDocument();
                    this.xmDocumentFile.Load(xmlFile);
                    this.LoadXmlNamespaceManager();
                }

                odsXmlDataProvider = new XmlDataProvider()
                {
                    Document = xmDocumentFile,
                    XmlNamespaceManager = this.nsmanager,
                    XPath = "/office:meta/meta:user-defined"
                };
                return odsXmlDataProvider;
            }
        }

Can anyone link me to a full example using this format?  Its kinda hard to piece together the examples I've read over the internet.  I need an example that uses namespaces.

Thanks!...


Code is like a box of chocolates!...



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>