I am currently trying to flatten a deep-structured XML document in C# so that every value of an element is converted to an attibute.
The XML structure is as follows:
<members>
<member xmlns="mynamespace" id="1" status="1">
<sensitiveData>
<notes/>
<url>someurl</url>
<altUrl/>
<date1>somedate</date1>
<date2>someotherdate</date2>
<description>some description</description>
<tags/>
<category>some category</category>
</sensitiveData>
<contacts>
<contact contactId="1">
<contactPerson>some contact person</contactPerson>
<phone/>
<mobile>mobile number</mobile>
<email>some@email.com</email>
</contact>
</contacts>
</member>
</members>
What I want it to look like is this:
<members>
<member xmlns="mynamespace" id="1" status="1" notes="" url="someurl" altUrl="" date1="somedate" date2="someotherdate" description="some description" tags="" category="some category" contactId="1" contactPerson="some contact person" phone="" mobile="mobile number" email="some@email.com" />
</members>
I could just parse away on the element names and their attributes, but since this XML comes from a webservice that I can’t control, I have to create some sort of dynamic parser to flatten this as the structure can change at some point.
Should be worth noting that the XML structure comes as an XElement from the webservice.
Has anyone tried to do this before and would be helpful to share how? 🙂 It would be greatly appreciated!
Thanks a lot in advance.
All the best,
Bo
Try this:
Result: