Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 11-Apr-17 20:02pm
v3

1 solution

Have you considered using a Dictionary for storing these values? Here is how you can build it from XML.

This code will take all the values tags and store all its descendants as a dictionary.

C#
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);
 
Share this answer
 
v2
Comments
sathish kumbanad 12-Apr-17 2:11am    
Thanks lw@zi for your quick reply.I will try it and let you know whether it is solve my issue or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900