濮阳杆衣贸易有限公司

主頁 > 知識庫 > ASP.NET中XML轉(zhuǎn)JSON的方法實例

ASP.NET中XML轉(zhuǎn)JSON的方法實例

熱門標(biāo)簽:征途美甲店地圖標(biāo)注 百度地圖怎樣做地圖標(biāo)注 浦發(fā)電話機器人提醒還款 柳州電銷機器人公司 電銷語音機器人型號參數(shù) 太原400電話上門辦理 騰訊地圖標(biāo)注手機 昆明語音電銷機器人價格 400電話如何申請取消

本文實例講述了ASP.NET中XML轉(zhuǎn)JSON的方法,分享給大家供大家參考。具體如下:

一般在許多應(yīng)用程序中都將數(shù)據(jù)存儲為XML的格式,而且會將數(shù)據(jù)以JSON的格式發(fā)送到客戶端以做進一步處理。要實現(xiàn)這一點,它們必須將XML格式轉(zhuǎn)換為JSON格式。

XML轉(zhuǎn)JSON代碼如下:

復(fù)制代碼 代碼如下:
private static string XmlToJSON(XmlDocument xmlDoc) 

    StringBuilder sbJSON = new StringBuilder(); 
    sbJSON.Append("{ "); 
    XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true); 
    sbJSON.Append("}"); 
    return sbJSON.ToString(); 

 
//  XmlToJSONnode:  Output an 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 that we know whether the nodeName is an 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 that we know whether the nodeName is an 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(); 
}

希望本文所述對大家的asp.net程序設(shè)計有所幫助。

您可能感興趣的文章:
  • xml轉(zhuǎn)json的js代碼
  • C# XML與Json之間相互轉(zhuǎn)換實例詳解
  • 對比分析json及XML
  • json跟xml的對比分析
  • JavaScript將XML轉(zhuǎn)成JSON的方法
  • JavaScript原生xmlHttp與jquery的ajax方法json數(shù)據(jù)格式實例
  • 如何在JS中實現(xiàn)相互轉(zhuǎn)換XML和JSON

標(biāo)簽:白山 張家界 德陽 江蘇 蘭州 天門 新疆 陽泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET中XML轉(zhuǎn)JSON的方法實例》,本文關(guān)鍵詞  ASP.NET,中,XML,轉(zhuǎn),JSON,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET中XML轉(zhuǎn)JSON的方法實例》相關(guān)的同類信息!
  • 本頁收集關(guān)于ASP.NET中XML轉(zhuǎn)JSON的方法實例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    威信县| 揭阳市| 泗阳县| 竹溪县| 新野县| 平果县| 鹤山市| 天镇县| 西藏| 荆门市| 天气| 无棣县| 百色市| 盐山县| 大同市| 九龙坡区| 东至县| 南江县| 镇巴县| 白山市| 独山县| 金沙县| 开阳县| 黄平县| 新竹县| 临沧市| 环江| 呈贡县| 曲阳县| 泰宁县| 石阡县| 绥化市| 鹤岗市| 绵阳市| 大港区| 凤冈县| 行唐县| 淅川县| 勐海县| 潞城市| 安平县|