I have a stored procedure that returns variable data from an EAV database and want to export the data as an XML.
DataTable columns:
group varchar
attribute varchar
value varchar
Datatable Example:
group attribute value
General Name Big Bird
BodyType Color Yellow
BodyType Height 200
General Age 5
Desired XML:
<RootTag>
<General>
<Name>Big Bird</Name>
<Age>5</Age>
</General
<BodyType>
<Color>Yellow</Color>
<Height>200</Height>
</BodyType>
</RootTag>
If I do a DataTable.WriteXml I get:
<Item>
<group>General</group>
<attribute>Name</attribute>
<value>Big Bird</value>
</Item>
<Item>
<group>BodyType</group>
<attribute>Color</attribute>
<value>Yellow</value>
</Item>
...
I am currently building a type dynamically at run-time with reflection and serializing with that type. I'm thinking there has to be a better way.
EDIT: DataTable.WriteXml sample
public static string BuildFullXML(DataTable OrderData, string OutPath)
{
if (!Directory.Exists(OutPath)) { Directory.CreateDirectory(OutPath); }
OutPath += DateTime.Now.Ticks + ".xml";
OrderData.TableName = "Item";
using (TextWriter TW = File.CreateText(OutPath))
{
OrderData.WriteXml(TW);
TW.Close();
}
return path;
}