两个方法
上菜:
public string JsonToXml(string json)
{
string xml = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
try
{
XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max);
xmlDoc.Load(xmlReader);
var firstNode = xmlDoc.SelectNodes("root");
PrintChildNodes(firstNode);
}
catch (Exception ex)
{
}
return xmlDoc.OuterXml; //xml转string
}
public void PrintChildNodes(XmlNodeList childnodelist)
{
try
{
foreach (XmlNode node in childnodelist)
{
//(node.NodeType 是Text时,即是最内层 即innertext值,node.Attributes为null。
if (node.NodeType == XmlNodeType.Text)
{
continue;
}
node.Attributes.RemoveAll();
//捞取所有节点类型,名称,值
foreach (XmlAttribute atr in node.Attributes)
{
// Console.WriteLine("NodeType:" + atr.NodeType + "\t" + atr.Name + "\t:" + atr.Value);
}
if (node.ChildNodes.Count > 0)
{
PrintChildNodes(node.ChildNodes);
}
}
}
catch (Exception ex)
{
}
}
干净又卫生,兄弟们。