简体   繁体   English

如何从嵌套对象创建XML文档?

[英]How to create XML document from nested objects?

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. 我想从带有嵌套对象的对象中创建带有嵌套元素的xml文档,但是xml文件太平了。 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;
}

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. 如注释中所述,请注意,.NET包含将对象图转换为XML的功能,而无需编写任何代码即可生成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. 如果您希望对过程进行完全控制并希望使用反射,则Fasterflect包括将对象图转换为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 . 您可以在此源文件中找到XML扩展的代码 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. 您还需要为对象属性递归调用遍历方法,以便处理整个对象图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM