gpt4 book ai didi

c# - 在 XML 中选择特定节点列表

转载 作者:行者123 更新时间:2023-12-04 17:04:25 25 4
gpt4 key购买 nike

<Report xmlns="Microsoft.SystemCenter.DataWarehouse.Report.Alert" xmlns:p1="w3.org/2001/XMLSchema-instance"; Name="Microsoft.SystemCenter.DataWarehouse.Report.Alert" p1:schemaLocation="Microsoft.SystemCenter.DataWarehou?Schema=True">
<Title>Alert Report</Title>
<Created>6/27/2013 9:32 PM</Created>
<StartDate>6/1/2013 9:29 PM</StartDate>
<EndDate>6/27/2013 9:29 PM</EndDate>
<TimeZone>(UTC)</TimeZone>
<Severity>Warning, Critical</Severity>
<Priority>Low, Medium, High</Priority>
<AlertTable>
<Alerts>
<Alert>
<AlertName></AlertName>
<Priority></Priority>
</Alert>
</Alerts>
</AlertTable>
</Report>

所以我试图拉下出现在警报子项下的节点列表。所以/Report/AlertTable/Alerts.
我以前做过非常相似的事情,但在这种格式下它由于某种原因不起作用。有人可以指出我正确的方向吗?
  XmlDocument Log = new XmlDocument();
Log.Load("test.xml");
XmlNodeList myLog = Log.DocumentElement.SelectNodes("//Report/AlertTable/Alerts");

foreach (XmlNode alert in myLog)
{
Console.Write("HERE");
Console.WriteLine(alert.SelectNodes("AlertName").ToString());
Console.WriteLine(alert.SelectNodes("Priority").ToString());
Console.Read();
}

编辑:
其中一个回应让我尝试在 p1 中使用一堆命名空间,但没有这样的运气。

编辑:
也没有工作:
    var name = new XmlNamespaceManager(log.NameTable);
name.AddNamespace("Report", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList xml = log.SelectNodes("//Report:Alerts", name);

最佳答案

从一个网站:

nodename    Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are

所以我相信
"/AlertTable/Alerts"

会工作,因为那将是“从根节点”以及
"Report/AlertTable/Alerts"

XPath Site

关于c# - 在 XML 中选择特定节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17434672/

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