Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
JavaScript
{"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.
C#
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
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:
JavaScript
{"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"}
Posted
Updated 7-May-15 21:02pm
v2

This is nonsense. XML document, by definition, has one and only one root. If you remove the root, the next level of hierarchy can be one element of more than one. If there is only one element, removing root will give you valid XML (or at least well-formed). This is not the case in your example, so it cannot be XML.

—SA
 
Share this answer
 
Comments
CPallini 8-May-15 2:41am    
5.
Sergey Alexandrovich Kryukov 8-May-15 2:51am    
Thank you Carlo.
—SA
Joshua Masa 8-May-15 2:42am    
I know that XML needs root but my web api does not accept root. It accepts {"ExtSerial":"KDI2015050501","Date":"02/01/2015", ...} I need to pass that JSON to my web api without root.
Sergey Alexandrovich Kryukov 8-May-15 2:55am    
Your question was about XML root; and I answered it. And this is what your exceptions tells you. Note that "XML needs root" is not what I wrote for you. As to the question about JSON passing, I'm afraid your "I need to pass" is just your fantasy. If you explain the bigger picture, perhaps I'll be able to tell you what I do. But I would expect you to accept my answer.
—SA
Joshua Masa 8-May-15 2:52am    
Change question: How do I convert/Serialize my XML to JSON without including the root element?
If you want to skip root element and get only child nodes, use this:
C#
var invoices = xdoc.Root.Elements();


Now, you can "convert" it into json object.

Note, that using your code you'll get something like this:
{"ExtSerial":"KDI2015050501"},{"Date":"02/01/2015"},...

but not
{"ExtSerial":"KDI2015050501","Date":"02/01/2015",...}


I'd suggest to read this: Working with JSON Data[^]
 
Share this answer
 
Comments
Joshua Masa 8-May-15 3:37am    
This is better.
Maciej Los 8-May-15 3:38am    
Thanks, Joshua. Cheers, Maciej.
Sergey Alexandrovich Kryukov 8-May-15 3:45am    
5ed.
—SA
Maciej Los 8-May-15 5:28am    
Thank you Sergey ;)

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