【问题标题】:Unable to get list from xml using xPathNavigator无法使用 xPathNavigator 从 xml 获取列表
【发布时间】:2016-12-05 01:37:13
【问题描述】:
List<string> list = new List<string>();
foreach (XPathNavigator node in nav.Select("configuration/company/work/worktime"))
            {
                string day = getAttribute(node, "day");
                string time = getAttribute(node, "time");
                string worktype = ?? // how to get worktype attribute valuefrom parent node 
              list.Add(day,time,worktype); // add to list 
            }

 </configuration>
      <company>
        <work worktype="homeWork">
            <worktime day="30" time="10:28"></worktime>
            <worktime day="25" time="10:50"></worktime>
         </work>
        <work worktype="officeWork">
            <worktime day="12" time="09:28"></worktime>
            <worktime day="15" time="12:28"></worktime>
        </work>
      </company>
    </configuration>


need output as :
list[0] = homeWork,30,10:28
list[1] = homeWork,25,10:50
list[2] = officeWork,12,09:28
list[3] = officeWork,15,12:28

我正在尝试从 XML 中获取列表,但未能获得上面给出的输出(使用 xpath 导航器,如何访问父节点以获取工作类型属性和其他剩余的内部节点属性?

【问题讨论】:

    标签: c# xml list xpathnavigator


    【解决方案1】:

    使用嵌套循环。最初使用configuration/company/work 检索工作节点。检索工作类型属性并存储在变量中。然后循环遍历子工作类型节点并为每个节点添加一个字符串

    【讨论】:

    • 尝试了同样的方法,但是当我为配置/公司/工作/工作时间执行 foreach 循环时,它会为相同的工作类型添加双重输入,这意味着上面是 homeWork 的 2 条记录 - 我得到了 4 条记录,其中包括它自己的 2 条记录和另外 2 条 officeWork 记录
    【解决方案2】:

    使用网络库增强 xml (linq xml)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                var results = doc.Descendants("work").Select(x => new {
                    worktype = (string)x.Attribute("worktype"),
                    worktime = x.Elements("worktime").Select(y => new {
                        day = (int)y.Attribute("day"),
                        time = (DateTime)y.Attribute("time")
                    }).ToList()
                }).ToList();
            }
        }
    }
    

    【讨论】:

    • 感谢它的工作,但我必须使用 xPathNavigator 来完成
    • 我不喜欢使用可能包含病毒的第三方 dll。
    • @jdweng 是的,Microsoft 的 System.Xml.dll 通常带有病毒 ;)
    【解决方案3】:

    我建议在 XPath 上使用 LINQ to XML,但如果必须使用 XPathNavigator,则需要迭代每个 work 元素,然后是每个 worktime 子元素。这样您就可以使用父上下文中的worktype

    foreach (XPathNavigator work in nav.Select("configuration/company/work"))
    {
        var workType = work.GetAttribute("worktype", string.Empty);
    
        foreach (XPathNavigator worktime in work.Select("worktime"))
        {
            var day = worktime.GetAttribute("day", string.Empty);
            var time = worktime.GetAttribute("time", string.Empty);
    
            list.Add($"{workType}, {day}, {time}");
        }
    }
    

    请参阅this fiddle 以获得工作演示。

    【讨论】:

    • 冷却它的工作,你能解释一下为什么“string.Empty”被占用了
    • XML 中的名称由两部分组成:名称空间和本地名称。此处使用 string.Empty"" 是因为您的 XML 中的名称没有命名空间。
    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多