Hi,
I have a serialized XML that is converted to Json (Not sure if I said that right). It did the serialization successfully but I am going to be passing this Json to a web api to be posted via postman. Unfortunately, I need to get rid of root from the produced Json.
{"root":{"ExtSerial":"KDI2015050501","Date":"02/01/2015","CustomerRefNbr":"15-000001","Description":"2015 FEBRUARY RENTAL","Customer":"TRDA0139","Amount":"482072.5800","AQ_Branch":"KDI","AQ_COA":"4100503000","LineSubAccount":"OPMOPN000","LineTaxCategory":"False","LineQuantity":"1","LineUnitPrice":"482072.580000","AQ_PostStatus":"0","AQ_StatusDate":"02/01/2015","DTS":"02/01/2015","LineDescription":"Office Rental - Zero-Rated Sales"}}
If I remove root from the source XML it keeps sprouting an exception:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: There are multiple root elements. Line 2, position 2.
Here is my code so far. I have used Linq library and JSON.NET for serialization.
XDocument xdoc = XDocument.Load(@"D:\Documents\Projects\HeXML\tae.xml");
IEnumerable<XElement> Invoices = xdoc.Elements();
foreach (var Invoice in Invoices)
{
var json = JsonConvert.SerializeObject(Invoice);
Console.WriteLine(json);
}
Console.ReadLine();
I hope someone could help me.
Edit:
Here is the XML
<root>
<ExtSerial>KDI2015050501</ExtSerial>
<Date>02/01/2015</Date>
<CustomerRefNbr>15-000001</CustomerRefNbr>
<Description>2015 FEBRUARY RENTAL</Description>
<Customer>TRDA0139</Customer>
<Amount>482072.5800</Amount>
<AQ_Branch>KDI</AQ_Branch>
<AQ_COA>4100503000</AQ_COA>
<LineSubAccount>OPMOPN000</LineSubAccount>
<LineTaxCategory>False</LineTaxCategory>
<LineQuantity>1</LineQuantity>
<LineUnitPrice>482072.580000</LineUnitPrice>
<AQ_PostStatus>0</AQ_PostStatus>
<AQ_StatusDate>02/01/2015</AQ_StatusDate>
<DTS>02/01/2015</DTS>
<LineDescription>Office Rental - Zero-Rated Sales</LineDescription>
</root>
I just want to have it serialized to this JSON:
{"ExtSerial":"KDI2015050501","Date":"02/01/2015","CustomerRefNbr":"15-000001","Description":"2015 FEBRUARY RENTAL","Customer":"TRDA0139","Amount":"482072.5800","AQ_Branch":"KDI","AQ_COA":"4100503000","LineSubAccount":"OPMOPN000","LineTaxCategory":"False","LineQuantity":"1","LineUnitPrice":"482072.580000","AQ_PostStatus":"0","AQ_StatusDate":"02/01/2015","DTS":"02/01/2015","LineDescription":"Office Rental - Zero-Rated Sales"}