未知昵称 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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据