C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute)

复制代码

     ///
<summary> /// 遍历所有节点的type、name、Attributes /// </summary> /// <param name="childnodelist"></param> static void PrintChildNodes(XmlNodeList childnodelist) { try { foreach (XmlNode node in childnodelist) { //(node.NodeType 是Text时,即是最内层 即innertext值,node.Attributes为null。 if (node.NodeType == XmlNodeType.Text) { Console.WriteLine("NodeType:" + node.NodeType + "\t" + node.Name + "\t:" + node.Value); continue; } Console.WriteLine("==========node.Name:" + node.Name + "==========="); 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 ) { Console.WriteLine(ex.Message); } }
复制代码
       //实体类转Json字符串
      string strJson = JsonConvert.SerializeObject(inputModel);
      //Json转Xml文档
      XmlDocument xmlDocument = JsonConvert.DeserializeXmlNode(strJson);
      //选择Xml所有节点
      XmlNodeList xmlNodeList = xmlDocument.ChildNodes;

参考文档:C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute) - 宏宇 - 博客园 (cnblogs.com)

posted on   Violin_Huang  阅读(666)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统

导航

< 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

统计

点击右上角即可分享
微信分享提示