转换XML使用反射对象 [英] Convert XML to Object using reflection

查看:117
本文介绍了转换XML使用反射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你想问题要解决,在这里一个大单:D

If you like problems to resolve, here a big one :D

首先,这是不是系列化,确定

First, It isn't about serialization, ok?

好吧,我的情况...我书面方式,我将通过为参数的函数的的Xml(XmlDocument的)和对象(对象)作为参考。它会回到我充满了从XML(XmlDocument的)值的对象(被引用的对象)

Well, my situation... I am writting a function that I will pass as parameter a Xml (XmlDocument) and an object (Object) as reference. It will return to me a object (object that was referenced) filled with the values from the Xml (XmlDocument).

例如:

我有一个类似的xml:

I have a Xml like:

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



我也有我的功能

Also I have my function

public Object transformXmlToObject (XmlDocument xml, Object ref)
{
  // Scroll each parameters in Xml and fill the object(ref) using reflection.
  return ref;
}



我将如何使用它?

How will I use it?

我会用这样的:

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



我需要帮助球员请。

I need help guys, please.

最好的问候,

Best regards, Dan

推荐答案

嗯,是的,这正是有关序列。事实上,这正是XML序列化是为写的。

Erm, yes, this is exactly about serialization. In fact, this is exactly what XML serialization was written for.

无论如何,如果你想编写自己的,也许你可以根据你的XML标记设置属性BLOB?也就是说,如果你的用户对象有一个编号名称属性,也许你应该将它们按照XML BLOB ?

Anyway, if you want to write your own, perhaps you can set properties based on the tags in your XML blob? i.e. if you User object has an Id and a Name property, perhaps you should set them in accordance with the XML blob?

这篇关于转换XML使用反射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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