I have the following XML file that I need to convert to JSON. I am able to convert it to Json using Newtonsoft library but it includes xml declaration part also.How can i skip xml declaration part and convert remaining file to json?
I am using below code(C#) to convert it.
JsonConvert.SerializeXmlNode(employeeXMLDoc)
Sample xml input
<?xml version="1.0" encoding="UTF-8" ?>
<Employee>
<EmployeeID>1</EmployeeID>
<EmployeeName>XYZ</EmployeeName>
</Employee>
Json Output
{"?xml":{"@version":"1.0","@encoding":"UTF-8"},"Employee":{"EmployeeID":"1","EmployeeName":"XYZ"}}
You could remove the first child from the XmlDocument
:
employeeXMLDoc.RemoveChild(employeeXMLDoc.FirstChild);
And then serialize as you're doing now.
Or in a single line:
JsonConvert.SerializeXmlNode(employeeXMLDoc.FirstChild.NextSibling);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With