The mutual conversion between C# XML and Json

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

Preface

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""?>
                            <root>
                              <person id=""1"">
                              <name>Alan</name>
                              <url>http://www.google.com</url>
                              </person>
                              <person id=""2"">
                              <name>Louis</name>
                              <url> </url>
                              </person>
                            </root>";

This is a predefined XML string in code.

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

Then the string XML is read into XML document.

            string jsonText = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine(jsonText);

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"": ""http://www.google.com""
                                  },
                                  {
                                    ""@id"": ""2"",
                                    ""name"": ""Louis"",
                                    ""url"": ""http://www.yahoo.com""
                                  }
                                ]
                              }
                            }";

The Json string above a predefined

Also calls the Json.Net class in the method

            XmlDocument doc1 = JsonConvert.DeserializeXmlNode(json);
            Console.WriteLine(doc1.OuterXml);

 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);
                Console.WriteLine(doc2.OuterXml);

Results for the


Summary

 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