The mutual conversion between C# XML and Json

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


For this transformation is actually very simple, one of the most important is to reference library. Can go to the official website to download reference.

Two days before the study of the Jquery Ajax Json submission article link for background processing

XML is converted to Json string  

 string xml = @"<?xml version=""1.0"" standalone=""no""?>
                              <person id=""1"">
                              <person id=""2"">
                              <url> </url>

This is a predefined XML string in code.

            XmlDocument doc = new XmlDocument();

Then the string XML is read into XML document.

            string jsonText = JsonConvert.SerializeXmlNode(doc);

Then through the Json.Net class library and some methods for conversion to secondary school

The results of:

The Json string is converted to XML

Json first format string

            string json = @"{
                              ""?xml"": {
                                ""@version"": ""1.0"",
                                ""@standalone"": ""no""
                              ""root"": {
                                ""person"": [
                                    ""@id"": ""1"",
                                    ""name"": ""Alan"",
                                    ""url"": """"
                                    ""@id"": ""2"",
                                    ""name"": ""Louis"",
                                    ""url"": """"

The Json string above a predefined

Also calls the Json.Net class in the method

            XmlDocument doc1 = JsonConvert.DeserializeXmlNode(json);

 Json second format string

            string json_str = "{\"a\":\"a\",\"b\":\"b\"}";
            string json1 = @"{
              ""?xml"": {
                ""@version"": ""1.0"",
                ""@standalone"": ""no""
                ""root"":" + json_str + "}";

Similarly to call

                XmlDocument doc2 = JsonConvert.DeserializeXmlNode(json1);

Results for the


 The call is very simple, as long as the transfer of XML or Json format string to the right, then to get the results you want is not difficult. Code can directly call Copy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Byron at November 14, 2013 - 7:34 PM