How do I iterate through the XmlObjectBase object
Question asked by Holger Schlüter - February 12, 2018 at 12:29 PM
Unanswered
I would like to iterate recursively through the object created by 
    XmlObjectBase elm = syncAXISConfigSchema_InternalLib.ClassFactory.FromXmlFile(filename);
I would like to display all generated classes in a tree similar to the SimpleViewer in the SampleApplication.
I am looking for a collection like elm.AllChildren
 
 

2 Replies

Reply to Thread
1
I have been able to solve my problem using reflection.
Here is a code snippet that fills an XmlDocument (.NET) with the classnames of the tree:
 
        internal void ReadFile(string filename)
        {
            object elm = syncAXISConfigSchema_InternalLib.ClassFactory.FromXmlFile(filename);
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement element = xmlDocument.CreateElement(elm.GetType().Name);
            xmlDocument.AppendChild(element);
            ReflectData(elm, xmlDocument);
            OnDataChanged(xmlDocument);
        }
        internal void ReflectData(object item, XmlDocument xmlDocument)
        {
            Type type = item.GetType();
            foreach (PropertyInfo m in type.GetProperties())
            {
                if (m.PropertyType.Namespace == "cfg")
                {
                    var element = xmlDocument.CreateElement(m.PropertyType.Name);
                    var node = xmlDocument.SelectSingleNode("*");
                    node.AppendChild(element);
                    object newItem = Activator.CreateInstance(m.PropertyType);
                    ReflectData(newItem, xmlDocument);
                }
            }
        }
0
Liquid Support Replied
Employee Post
Yes, that is how the SimpleViewer works, but please note this approach can be slow for large documents as stated in the MessageBox when you run the SimpleViewer.

Reply to Thread