1

I'd like to created an xml document with nested elements from an object with nested objects but the xml file comes out too flat. How can I get this to iterate the objects within objects to create elements within elements.

public object traverse(object pc, string xpath, XmlDocument xmldoc)
{
    IEnumerable enumerable = pc as IEnumerable;
    if (enumerable != null)
    {
        foreach (object element in enumerable)
        {
            RecurseObject ro = new RecurseObject();
            ro.traverse(elementArray, xpath, xmldoc);
        }
    }
    else
    {                         
        Type arrtype = pc.GetType();
        string elementname = arrtype.Name;
        foreach (var prop in pc.GetType().GetProperties())
        {

            XmlElement xmlfolder = null;
            XmlNode xmlnode3 = null;
            string propname = prop.Name;
            string propvalue = "null";
            if (xmldoc.SelectSingleNode(xpath + "/" + elementname) == null)
            {
                xmlnode3 = xmldoc.SelectSingleNode(xpath);
                xmlfolder = xmldoc.CreateElement(null, elementname, null);
                xmlnode3.AppendChild(xmlfolder);

            }
            if (prop.GetValue(pc, null) != null)
            {
                propvalue = prop.GetValue(pc, null).ToString();
            }

            xmlnode3 = xmldoc.SelectSingleNode(xpath + "/" + elementname);
            xmlfolder = xmldoc.CreateElement(null, propname, null);
            xmlfolder.InnerText = propvalue;
            xmlnode3.AppendChild(xmlfolder);
        }
    }

    return null;
}
3
  • 1
    a) How is your complex object b) What kind of xml do you want to get at the end c) Have you read anything about xml serialization? Commented May 19, 2013 at 22:30
  • Even after formatting code it's not clear what exactly are you trying to achieve. Add examples of input data and desired output. Commented May 19, 2013 at 22:31
  • I4V is right. you can automatically generate XML from objects through serialization Commented May 19, 2013 at 22:41

1 Answer 1

1

As mentioned in the comments, be aware that .NET includes the capability to convert object graphs to XML without you having to write any of the code to generate the XML. This process is referred to as serialization, and it should be easy to find examples online or here at SO.

If you prefer complete control over the process and wish to use reflection, Fasterflect includes code to convert an object graph to XML. It's a library with helpers to make reflection easier and faster. You can find the code for the XML extensions in this source file. Be aware that the referenced implementation does detect or handle circular references, whereas the built-in serialization mechanisms do.

As for your own solution, you do not seem to have any code to detect whether a property value is itself an object or a primitive value. You need to call your traverse method recursively also for object properties in order to process the entire object graph.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.