Click here to Skip to main content
14,971,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to serialize XML to JSON. Getting the XML from a file location and running it to a foreach statement to serialize. But the thing is the output of the json is
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"}

Here is the XML:
JavaScript
<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>

It won't work without the root tag for some reason.
Here is my code:
C#
static void Main(string[] args)
{
    XElement xEle = XElement.Load(@"D:\Documents\Projects\HeXML\tae.xml");
    IEnumerable<XElement> invoices = xEle.Elements();

    foreach (var invoice in invoices)
    {
        Console.WriteLine(JsonConvert.SerializeXNode(invoice));
    }
    Console.ReadLine();
}

I'm sorry. I want to know where I went wrong.
Posted
Updated 7-May-15 5:33am
v2
Comments
BillWoodruff 7-May-15 12:04pm
   
Why aren't you de-serializing the XML into an instance of a Class, and then serializing that instance out with JSON ?
Joshua Masa 7-May-15 12:20pm
   
Sorry, I'm confused.
BillWoodruff 7-May-15 20:58pm
   
You may wish to try using xsd.exe and compare the result:

https://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

1 solution

Have a look at past answer: remotly convert xml to json in asp.net mvc[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900