国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

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

2024-07-10 12:48:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了ASP.NET中XML轉(zhuǎn)JSON的方法,。具體如下:

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

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

代碼如下: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("http://"" + SafeJSON(node.Name) + "http://": "); 
    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) 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平武县| 泸溪县| 玛纳斯县| 安国市| 涞水县| 安新县| 成都市| 都匀市| 昭通市| 大埔县| 阳信县| 台州市| 忻州市| 澄城县| 新田县| 库尔勒市| 平阳县| 资中县| 惠州市| 民县| 禹州市| 徐水县| 青阳县| 太和县| 呈贡县| 垫江县| 安顺市| 赤壁市| 长兴县| 邢台县| 勐海县| 建瓯市| 泊头市| 江津市| 全南县| 霞浦县| 汕头市| 巨野县| 金华市| 定州市| 抚顺县|