ASP.NET中XML转JSON的方法
作者:佚名    ASP.NET网站开发编辑:admin   更新时间:2022-07-23
asp.net中xml转JSON的方法
许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理。要实现这一点,它们必须将XML格式转换为JSON格式。
XML转JSON代码
[csharp]view plaincopy
- PRivatestaticstringXmlToJSON(XmlDocumentxmlDoc)
- {
- StringBuildersbJSON=newStringBuilder();
- sbJSON.Append("{");
- XmlToJSONnode(sbJSON,xmlDoc.DocumentElement,true);
- sbJSON.Append("}");
- returnsbJSON.ToString();
- }
- //XmlToJSONnode:OutputanXmlElement,possiblyaspartofahigherarray
- privatestaticvoidXmlToJSONnode(StringBuildersbJSON,XmlElementnode,boolshowNodeName)
- {
- if(showNodeName)
- sbJSON.Append("\\""+SafeJSON(node.Name)+"\\":");
- sbJSON.Append("{");
- //Buildasortedlistofkey-valuepairs
- //wherekeyiscase-sensitivenodeName
- //valueisanArrayListofstringorXmlElement
- //sothatweknowwhetherthenodeNameisanarrayornot.
- SortedListchildNodeNames=newSortedList();
- //Addinallnodeattributes
- if(node.Attributes!=null)
- foreach(XmlAttributeattrinnode.Attributes)
- StoreChildNode(childNodeNames,attr.Name,attr.InnerText);
- //Addinallnodes
- foreach(XmlNodecnodeinnode.ChildNodes)
- {
- if(cnodeisXmlText)
- StoreChildNode(childNodeNames,"value",cnode.InnerText);
- elseif(cnodeisXmlElement)
- StoreChildNode(childNodeNames,cnode.Name,cnode);
- }
- //Nowoutputallstoredinfo
- foreach(stringchildnameinchildNodeNames.Keys)
- {
- ArrayListalChild=(ArrayList)childNodeNames[childname];
- if(alChild.Count==1)
- OutputNode(childname,alChild[0],sbJSON,true);
- else
- {
- sbJSON.Append("\\""+SafeJSON(childname)+"\\":[");
- foreach(objectChildinalChild)
- OutputNode(childname,Child,sbJSON,false);
- sbJSON.Remove(sbJSON.Length-2,2);
- sbJSON.Append("],");
- }
- }
- sbJSON.Remove(sbJSON.Length-2,2);
- sbJSON.Append("}");
- }
- //StoreChildNode:StoredataassociatedwitheachnodeName
- //sothatweknowwhetherthenodeNameisanarrayornot.
- privatestaticvoidStoreChildNode(SortedListchildNodeNames,stringnodeName,objectnodeValue)
- {
- //Pre-processcontractionofXmlElement-s
- if(nodeValueisXmlElement)
- {
- //Convert<aa></aa>into"aa":null
- //<aa>xx</aa>into"aa":"xx"
- XmlNodecnode=(XmlNode)nodeValue;
- if(cnode.Attributes.Count==0)
- {
- XmlNodeListchildren=cnode.ChildNodes;
- if(children.Count==0)
- nodeValue=null;
- elseif(children.Count==1&&(children[0]isXmlText))
- nodeValue=((XmlText)(children[0])).InnerText;
- }
- }
- //AddnodeValuetoArrayListassociatedwitheachnodeName
- //IfnodeNamedoesn'texistthenaddit
- objectoValuesAL=childNodeNames[nodeName];
- ArrayListValuesAL;
- if(oValuesAL==null)
- {
- ValuesAL=newArrayList();
- childNodeNames[nodeName]=ValuesAL;
- }
- else
- ValuesAL=(ArrayList)oValuesAL;
- ValuesAL.Add(nodeValue);
- }
- privatestaticvoidOutputNode(stringchildname,objectalChild,StringBuildersbJSON,boolshowNodeName)
- {
- if(alChild==null)
- {
- if(showNodeName)
- sbJSON.Append("\\""+SafeJSON(childname)+"\\":");
- sbJSON.Append("null");
- }
- elseif(alChildisstring)
- {
- if(showNodeName)
- sbJSON.Append("\\""+SafeJSON(childname)+"\\":");
- stringsChild=(string)alChild;
- sChild=sChild.Trim();
- sbJSON.Append("\\""+SafeJSON(sChild)+"\\"");
- }
- else
- XmlToJSONnode(sbJSON,(XmlElement)alChild,showNodeName);
- sbJSON.Append(",");
- }
- //MakeastringsafeforJSON
- privatestaticstringSafeJSON(stringsIn)
- {
- StringBuildersbOut=newStringBuilder(sIn.Length);
- foreach(charchinsIn)
- {
- if(Char.IsControl(ch)||ch=='\\'')
- {
- intich=(int)ch;
- sbOut.Append(@"\\u"+ich.ToString("x4"));
- continue;
- }
- elseif(ch=='\\"'||ch=='\\\\'||ch=='/')
- {
- sbOut.Append('\\\\');
- }
- sbOut.Append(ch);
- }
- returnsbOut.ToString();
- }