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?
我有以下XML文件,我需要转换为JSON。我能够使用Newtonsoft库将其转换为Json,但它也包含xml声明部分。如何跳过xml声明部分并将剩余文件转换为json?
I am using below code(C#) to convert it.
我使用下面的代码(C#)来转换它。
JsonConvert.SerializeXmlNode(employeeXMLDoc)
Sample xml input
示例xml输入
<?xml version="1.0" encoding="UTF-8" ?>
<Employee>
<EmployeeID>1</EmployeeID>
<EmployeeName>XYZ</EmployeeName>
</Employee>
Json Output
Json输出
{"?xml":{"@version":"1.0","@encoding":"UTF-8"},"Employee":{"EmployeeID":"1","EmployeeName":"XYZ"}}
2 个解决方案
#1
5
You could remove the first child from the XmlDocument
:
您可以从XmlDocument中删除第一个子项:
employeeXMLDoc.RemoveChild(employeeXMLDoc.FirstChild);
And then serialize as you're doing now.
然后按照你现在的顺序进行序列化。
#2
2
Or in a single line:
或者在一行中:
JsonConvert.SerializeXmlNode(employeeXMLDoc.FirstChild.NextSibling);
JsonConvert.SerializeXmlNode(employeeXMLDoc.FirstChild.NextSibling);
#1
5
You could remove the first child from the XmlDocument
:
您可以从XmlDocument中删除第一个子项:
employeeXMLDoc.RemoveChild(employeeXMLDoc.FirstChild);
And then serialize as you're doing now.
然后按照你现在的顺序进行序列化。
#2
2
Or in a single line:
或者在一行中:
JsonConvert.SerializeXmlNode(employeeXMLDoc.FirstChild.NextSibling);
JsonConvert.SerializeXmlNode(employeeXMLDoc.FirstChild.NextSibling);