c# - 如何在选择节点中遍历 XML 中的所有元素

标签 c# xml xpath namespaces selectsinglenode

我真的是 c# 的新手。我正在创建一个需要从 XML 文件中提取数据的项目。我可以选择一个节点并循环遍历该元素的内部文本。但是我在 XML 文件中有几个元素。我想在一列中一次循环遍历所有元素。我怎么能这样做。我正在详细解释。

这是我的 XML 文件。

    <?xml version="1.0" encoding="utf-8"?>
<tlp:WorkUnits xmlns:tlp="http://www.timelog.com/XML/Schema/tlp/v4_4"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.timelog.com/XML/Schema/tlp/v4_4 http://www.timelog.com/api/xsd/WorkUnitsRaw.xsd">
  <tlp:WorkUnit ID="130">
    <tlp:EmployeeID>3</tlp:EmployeeID>
    <tlp:AllocationID>114</tlp:AllocationID>
    <tlp:TaskID>239</tlp:TaskID>
    <tlp:ProjectID>26</tlp:ProjectID>
    <tlp:ProjectName>LIK Template</tlp:ProjectName>
    <tlp:CustomerId>343</tlp:CustomerId>
    <tlp:CustomerName>Lekt Corp Inc.</tlp:CustomerName>
    <tlp:IsBillable>1</tlp:IsBillable>
    <tlp:ApprovedStatus>0</tlp:ApprovedStatus>
    <tlp:LastModifiedBy>AL</tlp:LastModifiedBy>
  </tlp:WorkUnit>
  <tlp:WorkUnit ID="131">
    <tlp:EmployeeID>3</tlp:EmployeeID>
    <tlp:AllocationID>114</tlp:AllocationID>
    <tlp:TaskID>239</tlp:TaskID>
    <tlp:ProjectID>26</tlp:ProjectID>
    <tlp:ProjectName>LIK Template</tlp:ProjectName>
    <tlp:CustomerId>343</tlp:CustomerId>
    <tlp:CustomerName>Lekt Corp Inc.</tlp:CustomerName>
    <tlp:IsBillable>1</tlp:IsBillable>
    <tlp:ApprovedStatus>0</tlp:ApprovedStatus>
    <tlp:LastModifiedBy>AL</tlp:LastModifiedBy>
  </tlp:WorkUnit>

这是我的 ConsumeReportingApi 类,我想在其中迭代值。

    public class ConsumeReportingApi
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof (ConsumeReportingApi));

        public static void Consume()
        {
            if (ServiceHandler.Instance.TryAuthenticate())
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Successfully authenticated on reporting API");
                }

               var customersRaw = ServiceHandler.Instance.Client.GetWorkUnitsRaw(ServiceHandler.Instance.SiteCode,
                    ServiceHandler.Instance.ApiId,
                    ServiceHandler.Instance.ApiPassword,
                    WorkUnit.All,
                    Employee.All,
                    Allocation.All,
                    Task.All,
                    Project.All,
                    Department.All,
                    DateTime.Now.AddDays(-5).ToString(),
                    DateTime.Now.ToString()
                 );

                if (customersRaw.OwnerDocument != null)
                {
                    var namespaceManager = new XmlNamespaceManager(customersRaw.OwnerDocument.NameTable);
                    namespaceManager.AddNamespace("tlp", "http://www.timelog.com/XML/Schema/tlp/v4_4");

                    var customers = customersRaw.SelectNodes("tlp:WorkUnit", namespaceManager);
                    if (customers != null)
                    {
                        foreach (XmlNode customer in customers)
                        {
                             var taskId = customer.SelectSingleNode("tlp:TaskId", namespaceManager);
                          if (taskId != null)
                          {
                              if (Logger.IsDebugEnabled)
                              {
                                  Logger.Debug("Task ID "+taskId.InnerText);
                              }
                          }
                          var department = customer.SelectSingleNode("tlp:ProjectName", namespaceManager);
                          if (department != null)
                          {
                              if (Logger.IsDebugEnabled)
                              {
                                  Logger.Debug("Department ID: "+department.InnerText);
                              }
                          }
        }
    }
}

此时我在customersRaw.SelectNodes方法中设置的是tlp:TaskID。但我想在那里获取所有其他元素 os xml 并打印这些值。但我不知道该怎么做。

我的主程序类是

public class Program
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(Program));
    private static readonly DirectoryInfo AppPath = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;

    public static void Main(string[] args)
    {
   ....

        try
        {
            if (Logger.IsInfoEnabled)
            {
                Logger.Info("Running application");
            }

            // Run example classes
            ConsumeReportingApi.Consume();

        }
        catch (Exception ex)
        {
            ...
        }

        if (Logger.IsInfoEnabled)
        {
            Logger.Info("---");
            Logger.Info("Application loop ended. Click to exit");
        }

        // Wait for the user to end the application.
        Console.ReadKey();
    }
}

最佳答案

如果你想记录所有子节点的内部文本,试试这个:

var customers = customersRaw.SelectNodes("tlp:WorkUnit", namespaceManager);
if (customers != null)
{
    foreach (XmlNode customer in customers)
    {
        var childNodes = customer.SelectNodes("./*");
        if(childNodes != null)
        {
            foreach(XmlNode childNode in childNodes)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug(customerName.InnerText);
                }
            }
        }
    }
}

如果你想得到一个你知道名字的特定节点的内部文本,你可以使用SelectSingleNode:

var childNode = customer.SelectSingleNode("./tlp:CustomerId", namespaceManager);
if(childNode != null)
    Debug.WriteLine(childNode.InnerText);

关于c# - 如何在选择节点中遍历 XML 中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47755321/

相关文章:

xml - 使用 XSLT 递归删除空的 xml 元素

java - 软键盘覆盖 PopupWindow 中的 EditText

xml - 如何使用 XSL 复制子项

python - Scrapy:获取两个标识符之间的文本

Python Selenium Webdriver Wait.Until 显示错误正好需要 2 个参数 3

c# - 分析大量数据的有效方法?

c# - 如何将 C# 中的 Type 类对象序列化和反序列化为 YAML?

c# - 来自 C# 的 Azure BLOB 存储 REST 调用

c# - 我如何通过哈希码找到一个对象?

xml - 删除 XSLT 中的前导 0