欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中XML转JSON的方法实例

程序员文章站 2024-02-23 23:55:40
本文实例讲述了asp.net中xml转json的方法,分享给大家供大家参考。具体如下: 一般在许多应用程序中都将数据存储为xml的格式,而且会将数据以json的格式发送到...

本文实例讲述了asp.net中xml转json的方法,分享给大家供大家参考。具体如下:

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

xml转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("\\"" + 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程序设计有所帮助。