This is a migrated thread and some comments may be shown as answers.

XML to ObservableCollection

0 Answers 222 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Blair
Top achievements
Rank 1
Blair asked on 29 May 2018, 01:44 PM

Hi, I'm attempting to use the following example to read in my XML:

https://docs.telerik.com/devtools/wpf/controls/radtreeview/populating-with-data/data-binding-to-xml

 

I am finding that when I convert my items to an ObservableCollection, no results are returned.

 

My XML is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<DocDef Filename="X">
  <DocVars Selected="yes">
    <DocVar Name="JobNumber" Value="0000" Selected="yes"/>
    <DocVar Name="ProjectID" Value="0000" Selected="yes"/>
  </DocVars>
  <DocContent Name="Requirements" Selected="False">
    <DocContent Name="Meeting" Selected="True" />
    <DocContent Name="Quality" Selected="True" />
    <DocContent Name="OHS" Selected="False">
      <DocContent Name="Another thing" Selected="False"/>
    </DocContent>
  </DocContent>
  <DocContent Name="Footer"/>
</DocDef>

 

And my associated XML serialization:

    public class GenericNode
    {
        [XmlAttribute("Name")]
        public String Name { get; set; }

        [XmlAttribute("Selected")]
        public String Selected { get; set; }
    }

    [XmlRoot(ElementName = "DocDef")]
    public class DocDef : GenericNode
    {
        [XmlArray("DocVars")]
        [XmlArrayItem("DocVar", Type = typeof(DocVar))]
        public List<GenericNode> DocVarItems { get; set; }

        [XmlElement("DocContent")]
        public List<DocContentValue> DocContentItems { get; set; }
    }
    
    public class DocVar : GenericNode { }

    public class DocContentValue : GenericNode
    {
        [XmlElement("DocContent")]
        public List<DocContentValue> DocContentItems { get; set; }

    }

 

My issue arises when attempting to extend DocDef as an ObservableCollection<GenericNode> and adding the "AddRange" function:

[XmlRoot(ElementName = "DocDef")]

public class DocDef : ObservableCollection<GenericNode>

{

    public void AddRange( IEnumerable<GenericNode> range )

     {

        foreach (GenericNode node in range)

        {

            this.Add( node );

        }

    }

    ...

If I follow through the rest of the example, AddRange does not add any elements:

 

public class RadTreeViewXmlDataSource : DocDef
    {
        private string source;
        public string Source
        {
            get
            {
                return this.source;
            }
            set
            {
                this.source = value;
                AddRange( RetrieveData( Application.GetResourceStream( new Uri( value, UriKind.Relative ) ).Stream ) );
            }
        }
        private DocDef RetrieveData( Stream xmlStream )
        {
            XmlSerializer serializer = new XmlSerializer( typeof( DocDef ) );
            StreamReader reader = new StreamReader( xmlStream );
            DocDef list = ( DocDef )serializer.Deserialize( reader );
            return list;
        }
    }

 

Any help would be very much appreciated!

 

 

 

No answers yet. Maybe you can help?

Tags
TreeView
Asked by
Blair
Top achievements
Rank 1
Share this question
or