For a C# project I’m querying an API, which returns me an XML similar to this:
<itemlist>
<item attrib1="Value1" attrib2="Value2"... attrib15="Value15">
<sometypeinfo1 attrib1="Value1" attrib2="Value2"... attrib15="Value15">
<subelement>
<someproperty attrib1="Value1" attrib2="Value2"/>
<someproperty attrib1="Value1" attrib2="Value2"/>
<someproperty attrib1="Value1" attrib2="Value2"/>
</subelement>
</sometypeinfo1>
<sometypeinfo2>
<subelement attrib1="Value1" attrib2="Value2">
<someproperty>
<somedescription attrib1="Value1" attrib2="Value2"/>
<somedescription attrib1="Value1" attrib2="Value2"/>
</someproperty>
</subelement>
</sometypeinfo2>
<sometypeinfo3 attrib1="Value1" attrib2="Value2"/>
<sometypeinfo4>
<someproperty attrib1="Value1" attrib2="Value2"/>
</sometypeinfo4>
<sometypeinfo5>
<someproperty attrib="somevalue"/>
</sometypeinfo5>
<somemodifiers>
<somemodifier attrib1="Value1" attrib2="Value2"/>
<somemodifier attrib1="Value1" attrib2="Value2"/>
<somemodifier attrib1="Value1" attrib2="Value2"/>
</somemodifiers>
<someflags>
<someflag attrib="somevalue"/>
<someflag attrib="somevalue"/>
<someflag attrib="somevalue"/>
</someflags>
</item>
<item>
.
.
.
</item>
</itemlist>
Its basically a list with ~ 100 items/file and every one with alot of descriptions, attributes etc.
Now, thats not something unusual.
I have trouble mapping it into a class or dataset. For example this line
<sometypeinfo1 attrib1="Value1" attrib2="Value2"... attrib15="Value15">
One item may miss attrib1, another attrib2, a 3rd one may have all 15 etc.
Same with “Someflags”, there can be one item with 5 “someflag”s, the next one only with 2.
etc.
Every element or attribut CAN be there, but doesnt has to. So they all share a pool of elements/attribs, and this is where I get stuck with serialization etc.
Yes, I’m new to this. But from what I’ve learned so far, a schema has to have all elements/attribs to have the XML being mapped properly?
The only thing that comes to mind would be writing another tool that collects all possible elements etc., then writing a class that contains all, NULL everything in the first place and then just parse the XML, overwriting everything found in the actual item.
Most XML parsing tools represent the attributes of an element as a collection, and you can iterate over that collection.
they also represent child elements as a collection too.
Here’s some sample syntax using my XML parsing class of choice, but c# has multiple build in options that you can explore at your leisure.
here’s a further documentation on XPATH that i used here.