How do I iterate through the XmlObjectBase object
Question asked by Holger Schlüter - February 12 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
 
 

1 Reply

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);
                }
            }
        }

Reply to Thread