欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年10月21日 10:37:52   投稿:shichen2014  
這篇文章主要介紹了ASP.NET中XML轉(zhuǎn)JSON的方法,實(shí)例講述了XML轉(zhuǎn)json的原理與實(shí)現(xiàn)過(guò)程,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下

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

一般在許多應(yīng)用程序中都將數(shù)據(jù)存儲(chǔ)為XML的格式,而且會(huì)將數(shù)據(jù)以JSON的格式發(fā)送到客戶(hù)端以做進(jìn)一步處理。要實(shí)現(xiàn)這一點(diǎ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(); 
}

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

相關(guān)文章

  • 利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之修復(fù)UEditor編輯時(shí)Bug(四)

    利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之修復(fù)UEditor編輯時(shí)Bug(四)

    這篇文章主要介紹了利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之修復(fù)UEditor編輯時(shí)Bug(四)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • DataTable多列合并問(wèn)題輕松搞定

    DataTable多列合并問(wèn)題輕松搞定

    由于題庫(kù)的表結(jié)構(gòu)不相同,導(dǎo)致同樣的Gridview在顯示時(shí)不能同時(shí)兩種不同結(jié)構(gòu)的數(shù)據(jù),這時(shí)如何在這個(gè)固定的GridView中顯示不同的數(shù)據(jù)呢,感興趣的朋友可以看下本文的解決方法
    2013-04-04
  • ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解

    ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解

    這篇文章主要介紹了ASP.NET頁(yè)面之間傳值的方式之Application實(shí)例詳解,需要的朋友可以參考下
    2017-10-10
  • asp.net core應(yīng)用docke部署到centos7的全過(guò)程

    asp.net core應(yīng)用docke部署到centos7的全過(guò)程

    這篇文章主要給大家介紹了關(guān)于asp.net core應(yīng)用docke部署到centos7的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 動(dòng)態(tài)代理的5模式使用示例和Mixin模式

    動(dòng)態(tài)代理的5模式使用示例和Mixin模式

    什么叫"動(dòng)態(tài)代理",代理模式我們都知道,動(dòng)態(tài)代理就是動(dòng)態(tài)生成的代理(采用Emit)。5種代理模式:ClassProxy、ClassProxyWithTarget、InterfaceProxyWithoutTarget、InterfaceProxyWithTarget、InterfaceProxyWithTargetInterface、Mixin模式
    2013-11-11
  • 使用ASP.NET?Web?API構(gòu)建Restful?API

    使用ASP.NET?Web?API構(gòu)建Restful?API

    這篇文章介紹了使用ASP.NET?Web?API構(gòu)建Restful?API的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • asp.net基礎(chǔ)學(xué)習(xí)之前端頁(yè)面布局

    asp.net基礎(chǔ)學(xué)習(xí)之前端頁(yè)面布局

    這篇文章主要為大家詳細(xì)介紹了asp.net基礎(chǔ)學(xué)習(xí)之前端頁(yè)面布局,什么是母版頁(yè),如何創(chuàng)建母版頁(yè),感興趣的小伙伴們可以參考一下
    2016-08-08
  • asp.net生成縮略圖示例方法分享

    asp.net生成縮略圖示例方法分享

    asp.net快速生成清晰縮略圖,大家參考使用吧
    2013-12-12
  • asp.net下日期加減的方法

    asp.net下日期加減的方法

    asp.net下日期加減的方法...
    2007-09-09
  • .Net Core靜態(tài)文件資源的使用

    .Net Core靜態(tài)文件資源的使用

    這篇文章介紹了.Net Core靜態(tài)文件資源的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論