-1

这是示例xml

<?xml version="1.0" encoding="utf-16"?>
<ServerInfoArrayResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Result xmlns="http://www.smartertools.com/smarterstats/ServerAdmin.asmx">true</Result>
  <ResultCode xmlns="http://www.smartertools.com/smarterstats/ServerAdmin.asmx">1</ResultCode>
  <Message xmlns="http://www.smartertools.com/smarterstats/ServerAdmin.asmx" />
  <Servers xmlns="http://www.smartertools.com/smarterstats/ServerAdmin.asmx">
    <ServerInfo>
      <ServerID>1234</ServerID>      
      <Name>server1234</Name>
    </ServerInfo>
</Servers>
</ServerInfoArrayResult>

我正在尝试获取 ServerID 和名称。这是我的代码。但它没有返回任何东西。

ar nsmgr = new XmlNamespaceManager(xDoc.NameTable);
    nsmgr.AddNamespace("a", "http://www.w3.org/2001/XMLSchema-instance");
    

    foreach (XmlNode xNode in xDoc.SelectNodes("/Servers/ServerInfo"))
    {
        //Add new row to the output buffer for each employee node in the XML file
        this.ServerInfoBuffer.AddRow();

        //Assign values to the columns.

        //Read the 1st attribute of the node Employee
        this.ServerInfoBuffer.ServerID = xNode.ChildNodes[0].InnerText;
        
        this.ServerInfoBuffer.ServerName = xNode.ChildNodes[1].InnerText;
        
    }
}

4

1 回答 1

0

这行得通。

 XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
        nsmgr.AddNamespace("a", "http://www.smartertools.com/smarterstats/ServerAdmin.asmx");

        foreach (XmlNode xNode in xDoc.SelectNodes("//ServerInfoArrayResult/a:Servers/a:ServerInfo", nsmgr))

于 2016-09-15T20:54:59.857 回答