如何从嵌套对象创建XML文档? [英] How to create XML document from nested objects?

查看:98
本文介绍了如何从嵌套对象创建XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从带有嵌套对象的对象中创建带有嵌套元素的xml文档,但是xml文件太平了.我该如何迭代对象内的对象以在元素内创建元素.

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

推荐答案

如注释中所述,请注意.NET包括将对象图转换为XML的功能,而无需编写任何代码即可生成XML. .此过程称为序列化,应该很容易在网上或在此处找到示例.

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.

如果您希望完全控制该过程并希望使用反射,则 Fasterflect 包含用于转换对象图的代码到XML.这是一个带有帮助程序的库,可帮助您更轻松,更快速地进行反思.您可以在此源文件中找到XML扩展的代码.请注意,引用的实现确实可以检测或处理循环引用,而内置的序列化机制却可以.

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.

这篇关于如何从嵌套对象创建XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆