未知昵称 2018-05-28 02:59 采纳率: 50%
浏览 880
已采纳

C#读取xml文件指定的值,一级一级往下寻找

手上有一个xml文件,希望能根据提供的xml文件寻找列出指定节点的所有内容,

例如寻找出ELEMENTS下的所有的值uint8,sint32,string,Earea,SRdsSettingInit.........等,
图片说明
就是能实现一级一级往下寻找,

如果存在同一级,列出该同级的所有的值,uint8,sint32,string,Earea,SRdsSettingInit.........等,

也希望能列出我想要的每一级指定的内容的值。
图片说明

xml文件下载地址

  • 写回答

7条回答 默认 最新

  • threenewbee 2018-05-28 03:33
    关注

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    namespace Q690372
    {
    class Program
    {
    static string getpath(XElement node)
    {
    string s = node.Name.ToString().Replace("{http://autosar.org/schema/r4.0}", "");
    while (node.Parent != null)
    {
    node = node.Parent;
    s = node.Name.ToString().Replace("{http://autosar.org/schema/r4.0}", "") + "/" + s;
    }
    return s;
    }

        static int getlevel(XElement node)
        {
            int n = 1;
            while (node.Parent != null)
            {
                node = node.Parent;
                n++;
            }
            return n;
        }
    
        static void Main(string[] args)
        {
            var doc = XDocument.Load(args[0]);
            foreach (var node in doc.Descendants())
            {
                if (getlevel(node) == 6 && node.Descendants().Count() == 0) //获得第六级的叶子节点
                    Console.WriteLine(getpath(node) + "=" + node.Value);
            }
        }
    }
    

    }

    AUTOSAR/AR-PACKAGES/AR-PACKAGE/AR-PACKAGES/AR-PACKAGE/SHORT-NAME=BaseTypes
    AUTOSAR/AR-PACKAGES/AR-PACKAGE/AR-PACKAGES/AR-PACKAGE/SHORT-NAME=ImplementationDataTypes
    AUTOSAR/AR-PACKAGES/AR-PACKAGE/AR-PACKAGES/AR-PACKAGE/SHORT-NAME=ApplicationDataTypes
    AUTOSAR/AR-PACKAGES/AR-PACKAGE/AR-PACKAGES/AR-PACKAGE/SHORT-NAME=CompuMethods
    AUTOSAR/AR-PACKAGES/AR-PACKAGE/ELEMENTS/SERVICE-INTERFACE/SHORT-NAME=Tuner
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。