序列化和反序列化未知XML [英] Serialize and deserialize unknown XML
问题描述
我有一个包含大数据的xml。我知道我的xml内容几乎有90%但剩下10%我需要动态序列化和反序列化。我知道该部分的类型。
示例xml
-----------
< Vehicle version =2.0>
< engines>
< Engine Name =Turbo/>
< Engine Name =Kappa/>
< cars>
< Car Name =XXEngine =TurboType =Premium>
< features>
< feature>
< attributename> TestAttribute
< type> Test1
< valuetype> TestFeature
< value>
< x> 100
< y> 200
< z> ; 200
< d> 400
< Car Name =YYEngine =Kappa>
< features>
< feature>
< attributename> TestAttribute
< type> Test1
< valuetype> TestFeature
< value>
XXXXX
< ;功能>
< attributename> TestAttribute
< type> YYY
< valuetype> TestXyz
< value>
< name> gdg
< age> drfd
这里标签内的值< value>将是动态的,我不是最初的内容,但我知道该对象的类型。
请任何人帮助解决这个问题。
我尝试了什么:
我有90%的xml数据类。
I have a xml with large data.I know almost 90% of my xml content but remaining 10 % I need to serialize and deserialize dynamically.I know the Type of that part.
Sample xml
-----------
<Vehicle version="2.0">
<engines>
<Engine Name="Turbo"/>
<Engine Name="Kappa"/>
<cars>
<Car Name="XX" Engine ="Turbo" Type="Premium">
<features>
<feature>
<attributename>TestAttribute
<type>Test1
<valuetype>TestFeature
<value>
<x>100
<y>200
<z>200
<d>400
<Car Name="YY" Engine ="Kappa">
<features>
<feature>
<attributename>TestAttribute
<type>Test1
<valuetype>TestFeature
<value>
XXXXX
<feature>
<attributename>TestAttribute
<type>YYY
<valuetype>TestXyz
<value>
<name>gdg
<age>drfd
Here the values inside the tag <value> will be be dynamic and I don't the content initially ,but I know the Type of that object.
Please anyone help to solve this issue.
What I have tried:
I have the data classes for that 90% of that xml.
推荐答案
您是否考虑使用字典
来存储这些值?以下是如何使用XML构建它。
此代码将采用所有值
标记并存储所有它的后代作为字典。
Have you considered using aDictionary
for storing these values? Here is how you can build it from XML.
This code will take all thevalues
tags and store all its descendants as a dictionary.
string xml = @"<start>
<valuetype>testFeature1</valuetype>
<values><a>1</a>2</values>
<valuetype>testFeature2</valuetype>
<values><x>10</x><y>20</y></values>
</start>";
XDocument xDocument = XDocument.Parse(xml);
Dictionary<string,string> dynamicThings = xDocument.Root.Elements()
.Where(x=> x.Name == "values")
.Descendants()
.Select(x => new { Key = x.Name, Value = x.Value })
.ToDictionary(x => x.Key.ToString(), x => x.Value);
这篇关于序列化和反序列化未知XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!