zoukankan
html css js c++ java
xml转换为json类
一个反编译的类,把xml转换为json的
调用:
XmlDocument doc
=
new
XmlDocument();
doc.LoadXml(
this
.textBox1.Text);
//
加载xml
this
.textBox2.Text
=
byLibrary.byComponent.XmlToJSON(doc);返回生成的json
Code
namespace
byLibrary
{
using
System;
using
System.Collections;
using
System.Text;
using
System.Xml;
public
class
byComponent
{
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(
"
,
"
);
}
private
static
string
SafeJSON(
string
sIn)
{
StringBuilder sbOut
=
new
StringBuilder(sIn.Length);
foreach
(
char
ch
in
sIn)
{
if
(
char
.IsControl(ch)
||
(ch
==
'
\
''
))
{
sbOut.Append(
@"
\u
"
+
((
int
) ch).ToString(
"
x4
"
));
}
else
{
if
(((ch
==
'
"
'
)
||
(ch
==
'
\\
'
))
||
(ch
==
'
/
'
))
{
sbOut.Append(
'
\\
'
);
}
sbOut.Append(ch);
}
}
return
sbOut.ToString();
}
private
static
void
StoreChildNode(SortedList childNodeNames,
string
nodeName,
object
nodeValue)
{
ArrayList ValuesAL;
if
(nodeValue
is
XmlElement)
{
XmlNode cnode
=
(XmlNode) nodeValue;
if
(cnode.Attributes.Count
==
0
)
{
XmlNodeList children
=
cnode.ChildNodes;
if
(children.Count
==
0
)
{
nodeValue
=
""
;
}
else
if
((children.Count
==
1
)
&&
(children[
0
]
is
XmlText))
{
nodeValue
=
((XmlText) children[
0
]).InnerText;
}
}
}
object
oValuesAL
=
childNodeNames[nodeName];
if
(oValuesAL
==
null
)
{
ValuesAL
=
new
ArrayList();
childNodeNames[nodeName]
=
ValuesAL;
}
else
{
ValuesAL
=
(ArrayList) oValuesAL;
}
ValuesAL.Add(nodeValue);
}
public
static
string
XmlToJSON(XmlDocument xmlDoc)
{
StringBuilder sbJSON
=
new
StringBuilder();
sbJSON.Append(
"
{
"
);
XmlToJSONnode(sbJSON, xmlDoc.DocumentElement,
true
);
sbJSON.Append(
"
}
"
);
return
sbJSON.ToString();
}
private
static
void
XmlToJSONnode(StringBuilder sbJSON, XmlElement node,
bool
showNodeName)
{
if
(showNodeName)
{
sbJSON.Append(
"
\
""
+ SafeJSON(node.Name) +
"
\
"
:
"
);
}
sbJSON.Append(
"
{
"
);
SortedList childNodeNames
=
new
SortedList();
if
(node.Attributes
!=
null
)
{
foreach
(XmlAttribute attr
in
node.Attributes)
{
StoreChildNode(childNodeNames, attr.Name, attr.InnerText);
}
}
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);
}
}
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(
"
}
"
);
}
}
}
查看全文
相关阅读:
在X++中编译并执行C#脚本
XML的序列化读取方式
在Dynamics AX 2009中调用Crystal Reports
AspDotNetStorefront中事件处理页面开发的注意事项
WCF服务开发中的SecurityNegotiationException异常
Visual Studio 2010 RC版发布
初探.NET 4.0中的Entity Framework
入学十年
HP dv1606tn 笔记本安装Windows 7经验一则
在VS.NET2008中使用并发布Crystal Reports ActiveX组件
原文地址:https://www.cnblogs.com/xiangboren/p/1589003.html
最新文章
ASP.Net本地化/国际化解决方案原理和代码示例 无为而为
VSTS有Bug,分析数据库的维度和维度属性使用中文命名时候,作为报表参数会出错。虽然有解决办法但是头大。 无为而为
评论网上热贴:社会最可怕的不是没有关爱,而是没有公平 无为而为
WCF Data Services调试方法(转)
user32 AnimateWindow API 用法
Visual Basic中My对象顶层成员说明
WebClient 上传下载用法
Web.config加密——使用aspnet_regiis
VS统计代码行数
SQL好的写法(转)
热门文章
Web.config加密——基础知识
Web.config加密——使用编程方式方式
几款代码混淆器(DotFuscator, .NET Reactor, xenocode)(转)
【Dynamics AX 6】新的X++编辑器
Multisite Activation Wizard BP错误的修正方法
文件批处理系统 GULU
在非UI线程中改变UI控件属性的通用方法
根据给定的字段名获得字段值
2009年全国软件工程大会论文集
解读X++中的分号
Copyright © 2011-2022 走看看