Asp. NET XML to JSON Method Example _ practical skills

Source: Internet
Author: User
Tags net xml sin

This article illustrates the method of ASP.net XML to JSON and share it for everyone's reference. Specifically as follows:

Data is typically stored in XML format in many applications, and data is sent to the client in JSON format for further processing. To achieve this, they must convert the XML format to JSON format.

The XML goto JSON code is as follows:

Copy Code code as follows:
private static string Xmltojson (XmlDocument xmldoc)
{
StringBuilder Sbjson = new StringBuilder ();
Sbjson.append ("{");
Xmltojsonnode (Sbjson, xmldoc.documentelement, true);
Sbjson.append ("}");
return sbjson.tostring ();
}

Xmltojsonnode:output a XmlElement, possibly as part of a higher array
private static void Xmltojsonnode (StringBuilder Sbjson, XmlElement node, bool shownodename)
{
if (shownodename)
Sbjson.append ("\") + Safejson (node. Name) + "\": ");
Sbjson.append ("{");
Build a sorted list of key-value pairs
Where key is case-sensitive nodename
Value is an ArrayList of string or XmlElement
So, we know whether the nodename is a array or not.
SortedList childnodenames = new SortedList ();

ADD in all node attributes
if (node. Attributes!=null)
foreach (XmlAttribute attr in node. Attributes)
Storechildnode (childnodenames,attr. Name,attr. InnerText);

ADD in all nodes
foreach (XmlNode cnode in node. ChildNodes)
{
if (Cnode is XmlText)
Storechildnode (Childnodenames, "value", Cnode. InnerText);
else if (Cnode is XmlElement)
Storechildnode (Childnodenames, Cnode. Name, Cnode);
}

Now output all stored info
foreach (String childname in Childnodenames.keys)
{
ArrayList alchild = (ArrayList) childnodenames[childname];
if (Alchild.count = 1)
Outputnode (ChildName, alchild[0], Sbjson, true);
Else
{
Sbjson.append ("\" "+ Safejson (childname) +" \ ": [");
foreach (Object child in Alchild)
Outputnode (ChildName, Child, Sbjson, false);
Sbjson.remove (Sbjson.length-2, 2);
Sbjson.append ("],");
}
}
Sbjson.remove (Sbjson.length-2, 2);
Sbjson.append ("}");
}

Storechildnode:store data associated with each nodename
So, we know whether the nodename is a array or not.
private static void Storechildnode (SortedList childnodenames, String nodename, Object NodeValue)
{
Pre-process contraction of Xmlelement-s
if (NodeValue is XmlElement)
{
Convert <aa></aa> into "AA": null
<aa>xx</aa> into "AA": "XX"
XmlNode Cnode = (XmlNode) nodevalue;
if (Cnode. Attributes.count = 0)
{
XmlNodeList children = Cnode. ChildNodes;
if (children. count==0)
NodeValue = null;
else if (children. Count = = 1 && (children[0] is XmlText))
NodeValue = ((XmlText) (children[0)). InnerText;
}
}
ADD NodeValue to ArrayList associated with each nodename
If nodename doesn ' t exist then add it
Object ovaluesal = Childnodenames[nodename];
ArrayList valuesal;
if (ovaluesal = null)
{
Valuesal = new ArrayList ();
Childnodenames[nodename] = valuesal;
}
Else
Valuesal = (ArrayList) ovaluesal;
Valuesal.add (NodeValue);
}

private static void Outputnode (String ChildName, Object Alchild, StringBuilder Sbjson, bool shownodename)
{
if (Alchild = null)
{
if (shownodename)
Sbjson.append ("\" "+ Safejson (childname) +" \ ":");
Sbjson.append ("null");
}
else if (Alchild is string)
{
if (shownodename)
Sbjson.append ("\" "+ Safejson (childname) +" \ ":");
String Schild = (string) alchild;
Schild = Schild.trim ();
Sbjson.append ("\" "+ Safejson (Schild) +" \ ");
}
Else
Xmltojsonnode (Sbjson, (XmlElement) alchild, shownodename);
Sbjson.append (",");
}

Make a string safe for JSON
private static string Safejson (String sIn)
{
StringBuilder sbout = new StringBuilder (sin.length);
foreach (char ch in sIn)
{
if (Char.iscontrol (ch) | | | ch = = ' \ ")
{
int ich = (int) ch;
Sbout.append (@ "\\u" + ich.) ToString ("x4"));
Continue
}
else if (ch = = ' \ \] ' | | ch = = ' \\\\ ' | | | ch = = ')
{
Sbout.append (' \\\\ ');
}
Sbout.append (CH);
}
return sbout.tostring ();
}

I hope this article will help you with the ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.