使用反射将XML转换成对象

3

如果您喜欢解决问题,这里有一个大问题:D

首先,这不是关于序列化的,好吗?

嗯,我的情况是...我正在编写一个函数,我将其作为参数传递Xml(XmlDocument)和对象(Object)的引用。它会将通过Xml(XmlDocument)填充值的对象(已引用的对象)返回给我。

例如:

我有一个像下面的Xml:

<user>
  <id>1</id>
  <name>Daniel</name>
</user>

我有一个函数。
public Object transformXmlToObject (XmlDocument xml, Object ref)
{
  // Scroll each parameters in Xml and fill the object(ref) using reflection.
  return ref;
}

我将如何使用它?

我会像这样使用:

[WebMethod]
public XmlDocument RecebeLoteRPS(XmlDocument xml)
{
  // class user receive the object converted from the function
  User user = new User();
  user = transformXmlToObject(xml, user);

  // object filled 
}

大家好,我需要帮助。

最好的问候, 丹


1
听起来像是关于序列化的问题 - 具体来说是 XmlSerializer 序列化。但也许不是这样。 - Will A
4
这不是序列化吗? - Firoso
5个回答

6
这应该可以满足你的需求。
using System.Xml;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Collections;

namespace MyProject.Helpers
{
    public class XMLHelper
    {
        public static void HydrateXMLtoObject(object myObject, XmlNode node)
        {
            if (node == null)
            {
                return;
            }

            PropertyInfo propertyInfo;
            IEnumerator list = node.GetEnumerator();
            XmlNode tempNode;

            while (list.MoveNext())
            {
                tempNode = (XmlNode)list.Current;

                propertyInfo = myObject.GetType().GetProperty(tempNode.Name);

                if (propertyInfo != null) {
                    setProperty(myObject,propertyInfo, tempNode.InnerText);

                }
            }
        }
    }
}

1

嗯,是的,这与序列化有关。实际上,这正是XML序列化所编写的。

无论如何,如果你想自己编写,也许可以根据XML块中的标签设置属性?例如,如果您的用户对象具有IdName属性,则应根据XML块设置它们。


1

如果User是一个已经定义好的对象,其属性将用XML数据填充,则是一个XML序列化问题。

如果您想让User成为一个基于XML数据在运行时动态结构的对象,请查看.NET 4.0中的ExpandoObject。可以遍历XML树并构建等价的ExpandoObject实例树。


0

我会在@andyuk发布的答案中添加一些类型验证。因此,对于每个属性,它将查找属性类型,然后尝试转换xml值,以便不会引发任何异常。

但是,您仍然需要确保XML数据应具有与您要插入的属性值相同的数据类型。您可以通过xsd文件为您的xml文件执行此操作,或者如果您知道自己的数据并且不会更改,则可以忽略它。

                    var pi = entity.GetType( ).GetProperty( eleProperty.Name.LocalName );
                    if ( pi.PropertyType == typeof( DateTime ) )
                    {
                        DateTime val = DateTime.MinValue;
                        DateTime.TryParse( ele.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else if ( pi.PropertyType == typeof( Int32 ) )
                    {
                        int val = 0;
                        Int32.TryParse( eleProperty.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else if ( pi.PropertyType == typeof( bool ) )
                    {
                        bool val = false;
                        Boolean.TryParse( eleProperty.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else
                    {
                        pi.SetValue( entity, eleProperty.Value, null );
                    }

0

我同意,这确实与序列化有关,应该是你要寻找的内容。不过,为了激发你对轻松查询XML文档的兴趣,请看一下LINQ to XML


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接