<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>
경고 자식 아래에 나타나는 노드 목록을 끌어 내려고했습니다. 그래서/보고서/AlertTable/경고. 이전에 매우 비슷하게 해왔지만이 형식에서는 어떤 이유로 작동하지 않습니다. 누군가 올바른 방향으로 나를 가리킬 수 있습니까?XML의 특정 노드 목록 선택
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)와 네임 스페이스의 무리를 사용하려고했지만, 그런 행운이 없었다.
편집 : 작동하지 않았다 중 하나
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"
일하는 것이 생각
: 사이트에서var name = new XmlNamespaceManager(log.NameTable);
name.AddNamespace("Report", "http://www.w3.org/2001/XMLSchema-instance");
XmlNodeList xml = log.SelectNodes("//Report:Alerts", name);
언제든지 LINQ to XML을 사용할 수 있습니까? 그것은 모두 XmlDocument와 함께 할 수 있지만 XML에 LINQ를 사용하면 더 좋은 시간을 가질 수 있습니다 ... –
try .SelectNodes ("Report/AlertTable/Alerts"); – Jonesopolis
@JonSkeet - 나는 LINQ를 사용한 적이 없다고 말할 수 없습니다. C#에서 이와 비슷한 스크립트를 작성한 적이 있습니다. 그러나 네임 스페이스가 그 일을 망칠 것이라고 생각합니다. – user99999991