zoukankan      html  css  js  c++  java
  • ASP.NET中XML转JSON的方法

    许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理。要实现这一点,它们必须将XML格式转换为JSON格式。

     

     

    XML转JSON代码

     

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. private static string XmlToJSON(XmlDocument xmlDoc)  
    2. {  
    3.     StringBuilder sbJSON = new StringBuilder();  
    4.     sbJSON.Append("{ ");  
    5.     XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true);  
    6.     sbJSON.Append("}");  
    7.     return sbJSON.ToString();  
    8. }  
    9.   
    10. //  XmlToJSONnode:  Output an XmlElement, possibly as part of a higher array  
    11. private static void XmlToJSONnode(StringBuilder sbJSON, XmlElement node, bool showNodeName)  
    12. {  
    13.     if (showNodeName)  
    14.         sbJSON.Append("\"" + SafeJSON(node.Name) + "\": ");  
    15.     sbJSON.Append("{");  
    16.     // Build a sorted list of key-value pairs  
    17.     //  where   key is case-sensitive nodeName  
    18.     //          value is an ArrayList of string or XmlElement  
    19.     //  so that we know whether the nodeName is an array or not.  
    20.     SortedList childNodeNames = new SortedList();  
    21.   
    22.     //  Add in all node attributes  
    23.     if( node.Attributes!=null)  
    24.         foreach (XmlAttribute attr in node.Attributes)  
    25.             StoreChildNode(childNodeNames,attr.Name,attr.InnerText);  
    26.   
    27.     //  Add in all nodes  
    28.     foreach (XmlNode cnode in node.ChildNodes)  
    29.     {  
    30.         if (cnode is XmlText)  
    31.             StoreChildNode(childNodeNames, "value", cnode.InnerText);  
    32.         else if (cnode is XmlElement)  
    33.             StoreChildNode(childNodeNames, cnode.Name, cnode);  
    34.     }  
    35.   
    36.     // Now output all stored info  
    37.     foreach (string childname in childNodeNames.Keys)  
    38.     {  
    39.         ArrayList alChild = (ArrayList)childNodeNames[childname];  
    40.         if (alChild.Count == 1)  
    41.             OutputNode(childname, alChild[0], sbJSON, true);  
    42.         else  
    43.         {  
    44.             sbJSON.Append(" \"" + SafeJSON(childname) + "\": [ ");  
    45.             foreach (object Child in alChild)  
    46.                 OutputNode(childname, Child, sbJSON, false);  
    47.             sbJSON.Remove(sbJSON.Length - 2, 2);  
    48.             sbJSON.Append(" ], ");  
    49.         }  
    50.     }  
    51.     sbJSON.Remove(sbJSON.Length - 2, 2);  
    52.     sbJSON.Append(" }");  
    53. }  
    54.   
    55. //  StoreChildNode: Store data associated with each nodeName  
    56. //                  so that we know whether the nodeName is an array or not.  
    57. private static void StoreChildNode(SortedList childNodeNames, string nodeName, object nodeValue)  
    58. {  
    59.     // Pre-process contraction of XmlElement-s  
    60.     if (nodeValue is XmlElement)  
    61.     {  
    62.         // Convert  <aa></aa> into "aa":null  
    63.         //          <aa>xx</aa> into "aa":"xx"  
    64.         XmlNode cnode = (XmlNode)nodeValue;  
    65.         if( cnode.Attributes.Count == 0)  
    66.         {  
    67.             XmlNodeList children = cnode.ChildNodes;  
    68.             if( children.Count==0)  
    69.                 nodeValue = null;  
    70.             else if (children.Count == 1 && (children[0] is XmlText))  
    71.                 nodeValue = ((XmlText)(children[0])).InnerText;  
    72.         }  
    73.     }  
    74.     // Add nodeValue to ArrayList associated with each nodeName  
    75.     // If nodeName doesn't exist then add it  
    76.     object oValuesAL = childNodeNames[nodeName];  
    77.     ArrayList ValuesAL;  
    78.     if (oValuesAL == null)  
    79.     {  
    80.         ValuesAL = new ArrayList();  
    81.         childNodeNames[nodeName] = ValuesAL;  
    82.     }  
    83.     else  
    84.         ValuesAL = (ArrayList)oValuesAL;  
    85.     ValuesAL.Add(nodeValue);  
    86. }  
    87.   
    88. private static void OutputNode(string childname, object alChild, StringBuilder sbJSON, bool showNodeName)  
    89. {  
    90.     if (alChild == null)  
    91.     {  
    92.         if (showNodeName)  
    93.             sbJSON.Append("\"" + SafeJSON(childname) + "\": ");  
    94.         sbJSON.Append("null");  
    95.     }  
    96.     else if (alChild is string)  
    97.     {  
    98.         if (showNodeName)  
    99.             sbJSON.Append("\"" + SafeJSON(childname) + "\": ");  
    100.         string sChild = (string)alChild;  
    101.         sChild = sChild.Trim();  
    102.         sbJSON.Append("\"" + SafeJSON(sChild) + "\"");  
    103.     }  
    104.     else  
    105.         XmlToJSONnode(sbJSON, (XmlElement)alChild, showNodeName);  
    106.     sbJSON.Append(", ");  
    107. }  
    108.   
    109. // Make a string safe for JSON  
    110. private static string SafeJSON(string sIn)  
    111. {  
    112.     StringBuilder sbOut = new StringBuilder(sIn.Length);  
    113.     foreach (char ch in sIn)  
    114.     {  
    115.         if (Char.IsControl(ch) || ch == '\'')  
    116.         {  
    117.             int ich = (int)ch;  
    118.             sbOut.Append(@"\u" + ich.ToString("x4"));  
    119.             continue;  
    120.         }  
    121.         else if (ch == '\"' || ch == '\\' || ch == '/')  
    122.         {  
    123.             sbOut.Append('\\');  
    124.         }  
    125.         sbOut.Append(ch);  
    126.     }  
    127.     return sbOut.ToString();  
    128. }  
  • 相关阅读:
    带勾选框的组织F4
    VBA 学习
    MACD指标量化策略实战案例
    DOM
    JS基础下
    JS基础
    CSS基础
    html实战4--transform3D
    html实战3--精灵图
    html实战2--四叶草
  • 原文地址:https://www.cnblogs.com/ranran/p/4039423.html
Copyright © 2011-2022 走看看