gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:07 32 4
gpt4 key购买 nike

我真的是 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com