简体   繁体   English

从xml和c#创建具有层次结构的动态对象

[英]Create dynamic object with hierarchy from xml and c#

I want to create a dynamic object from a string of XML. 我想从一串XML创建一个动态对象。 Is there an easy way of doing this? 有一个简单的方法吗?

Example String. 示例字符串。

<test><someElement><rep1>a</rep1><rep1>b</rep1></someElement></test>

I'm trying to create an mvc editor for passing data through nvelocity and would like people on the front end to input xml as there data for parsing. 我正在尝试创建一个用于通过nvelocity传递数据的mvc编辑器,并且希望前端的人输入xml作为要解析的数据。

Thanks in advance. 提前致谢。

You need 2 things to achieve this : 1) Valid xml 2) C# class which has same data members as in your input xml. 您需要完成以下两项工作:1)有效的xml 2)具有与输入xml中相同的数据成员的C#类。

You need to create one object of C# class then enumerate through all the elements of xml and when by using switch for each of the element name, you can take inner text property of that element and assign it to respective data member of object. 您需要创建一个C#类的对象,然后对xml的所有元素进行枚举,并且当对每个元素名称使用switch时,可以采用该元素的内部text属性并将其分配给对象的各个数据成员。 C# code might look like following (you need to fill in the gaps): C#代码可能类似于以下内容(您需要填补空白):

class test {
  List<string> someElement;
}

class xmlEnum 
{
 static test createObject(string inputXml) 
 {
     test t = new test();
     // load input xml in XmlDocument class
     // and start iterating thorugh all the elements
     swithc(elementName)
     {
        case rep1:
            t.someElement.add(element.innerText);
            break;
         // some more cases will go here

     }
   // finally return the object;
  return t;
 }
}

I hope this will help you. 我希望这能帮到您。

I don't think there's a ready-made dynamic solution to this. 我认为没有现成的动态解决方案。 If I understand your question correctly, you would like to do something like this. 如果我正确理解了您的问题,那么您想做这样的事情。

SomeDynamicXmlObject test = new SomeDynamicXmlObject(yourteststring);
var rep1 = test.SomeElement.rep1;

The closest I can think of you could get to that, is to use XElement classes, something like this: 我能想到的最接近的方法是使用XElement类,如下所示:

XElement test = XElement.Parse(yourteststring);
var rep1 = test.Element("SomeElement").Element("rep1");

If that's not good enough, I'm afraid you will have to write something yourself that will parse the xml and create the object on the fly. 如果这还不够好,恐怕您将不得不自己编写一些东西来解析xml并动态创建对象。 If you know in advance what the xml will look like, you could use shekhars code, but I guess from your comments that you don't. 如果您事先知道xml的外观,则可以使用shekhars代码,但是从您的评论中我猜您并没有。

If you have schema for xml available and if this is needed in dev/build environment then a round about way to do this will be 如果您有可用的xml模式,并且在开发/构建环境中需要使用xml模式,则将采用一种绕行方式

  1. Use XSD tool to parse schema and generate code from it 使用XSD工具解析架构并从中生成代码
  2. Build the generated code using command line complier or compiler services to generate assmebly. 使用命令行编译器或编译器服务来生成所生成的代码以生成汇编。 Now you have a type available there that can be used. 现在,您可以在那里使用可用的类型。 Needless to say this will be a quite slow and out-of-proc tools will be used here. 不用说这将是一个相当慢的过程,这里将使用过程外工具。

Another (not an easy way but faster) way that would not have dev env dependencies would be to parse your xml and generate dynamic type using reflection. 没有dev env依赖项的另一种(不是简单的方法,而是更快的)方法是解析xml并使用反射生成动态类型。 See this article to check how to use Reflection.Emit 请参阅本文以检查如何使用Reflection.Emit

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

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