保钠

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  26 随笔 :: 1 文章 :: 18 评论 :: 27969 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

两个方法

上菜:

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)
  {
  }
}

干净又卫生,兄弟们。

posted on   保钠  阅读(421)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示