如何在选择节点

时间:2017-12-11 14:41:42

标签: c# xml xpath namespaces selectsinglenode

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

这是我的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();
    }
}

}

2 个答案:

答案 0 :(得分:1)

如果要记录所有子节点的内部文本,请尝试此操作:

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);

答案 1 :(得分:0)

下面的代码将xml workunit解析为数据表,然后通过表进行交互以获取值

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement firstWorkUnit = doc.Descendants().Where(x => x.Name.LocalName == "WorkUnit").FirstOrDefault();
            XNamespace ns = firstWorkUnit.GetNamespaceOfPrefix("tlp");

            //create datatable
            DataTable dt = new DataTable();
            foreach (XElement col in firstWorkUnit.Elements())
            {
                dt.Columns.Add(col.Name.LocalName, typeof(string));
            }

            List<XElement> workUnits = doc.Descendants(ns + "WorkUnit").ToList();
            foreach (XElement workUnit in workUnits)
            {
                DataRow newRow = dt.Rows.Add();
                foreach(XElement col in workUnit.Elements())
                { 
                       newRow[col.Name.LocalName] = (string)col;
                }
            }

            List<string> projId = dt.AsEnumerable().Select(x => x.Field<string>("ProjectID")).ToList();

        }
    }
}