Subrata Sarkar
Subrata Sarkar

Reputation: 151

Covert XML string to json

I have a string xml

                    ?xml version="1.0" encoding="UTF-8"?>
                    <Entities TotalResults="64">
                       <Entity Type="test-set">
                          <ChildrenCount>
                             <Value>0</Value>
                          </ChildrenCount>
                          <Fields>
                             <Field Name="name">
                                <Value>default</Value>
                             </Field>
                             <Field Name="id">
                                <Value>0</Value>
                             </Field>
                          </Fields>
                          <RelatedEntities />
                       </Entity>
                       <Entity Type="test-set">
                          <ChildrenCount>
                             <Value>0</Value>
                          </ChildrenCount>                          
                          <RelatedEntities />
                       </Entity>
                       <singleElementCollection>false</singleElementCollection>
                    </Entities>

I am using below code to convert xml sting to json and used XMLDocument and JsonConvert.serializexmlNode().

XmlDocument doc = new XmlDocument();            
string xml = objHttpWebClient._retunResponseStream("http://test:8080/test-sets?fields=ID,Name", "GET", ASCIIEncoding.UTF8, cc);
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
return jsonText;

I am getting output like below

                                    {
                    "?xml": {
                        "@version": "1.0",
                        "@encoding": "UTF-8",
                        "@standalone": "yes"
                    },
                    "Entities": {
                        "@TotalResults": "64",
                        "Entity": [{
                            "@Type": "test-set",
                            "ChildrenCount": {
                                "Value": "0"
                            },
                            "Fields": {
                                "Field": [{
                                    "@Name": "name",
                                    "Value": "default"
                                },
                                {
                                    "@Name": "id",
                                    "Value": "0"
                                }]
                            },
                            "RelatedEntities": null
                        },
                        {
                            "@Type": "test-set",
                            "ChildrenCount": {
                                "Value": "0"
                            },

                        .........

...............................................................

however, I want to ignore '@' and { "?xml": { "@version": "1.0", "@encoding": "UTF-8", "@standalone": "yes" },

part from output json.

Upvotes: 0

Views: 1078

Answers (1)

Prashant Tiwari
Prashant Tiwari

Reputation: 344

You can simply remove the first child from the XML document before serializing it into json. It would look something like this.

doc.RemoveChild(doc.FirstChild);

This should do the trick.

Upvotes: 0

Related Questions