0
<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>

所以我试图下拉出现在 Alerts child 下的节点列表。所以 /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);
4

2 回答 2

1

从一个网站:

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 站点

于 2013-07-02T20:03:44.497 回答
0

想通了这个傻逼。它与“Microsoft.SystemCenter.DataWarehouse.Report.Alert”的命名空间有关。将其更改为任何内容,但不会正确读取 XML。

        XmlDocument log = new XmlDocument();
        log.Load(@"C:\Users\barranca\Desktop\test.xml");
       // XmlNodeList xml = log.SelectNodes("//ns1:Alerts");

        var name = new XmlNamespaceManager(log.NameTable);
        name.AddNamespace("ns1", "Microsoft.SystemCenter.DataWarehouse.Report.Alert");
        XmlNodeList xml = log.SelectNodes("//ns1:Alert", name);



        foreach (XmlNode alert in xml)
        {
            Console.Write("HERE");
            XmlNode test = alert.SelectSingleNode("//ns1:AlertName",name);
            string testing = test.InnerText;
            Console.Write(testing);
        }
于 2013-07-09T17:29:42.353 回答