2012-07-31 50 views
0

我一直在搜索這一段時間,找不到解決方案。我有一個從HttpWebRequest返回的xml,我加載到一個xmldocument中,我試圖獲取請求的特定屬性(狀態)。下面是返回的xml。在C中讀取xml屬性#

<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    > 
    <soapenv:Body> 
    <processRequestResponse> 
     <parameters> 
     <ns1:searchResponse status="success" xmlns:ns1="urn:oasis:names:tc:SPML:2:0:search"> 
      <ns1:pso> 
      <ns2:psoID ID="Users:####" xmlns:ns2="urn:oasis:names:tc:SPML:2:0"/> 
      <ns3:data xmlns:ns3="urn:oasis:names:tc:SPML:2:0"> 
       <ns4:attr name="Users.User ID" xmlns:ns4="urn:oasis:names:tc:DSML:2:0:core"> 
       <ns4:value></ns4:value> 
       </ns4:attr> 
      </ns3:data> 
      </ns1:pso> 
     </ns1:searchResponse> 
     </parameters> 
    </processRequestResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我正在使用我的代碼來獲取這些數據如下。

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
StreamReader reader = new StreamReader(response.GetResponseStream()); 
string returnResponse = reader.ReadToEnd(); 

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(returnResponse); 

XmlNode root = doc.DocumentElement; 
XmlNode searchResponse = root.SelectSingleNode("Envelope/Body/processRequestResponse/parameters/searchResponse"); 
XmlAttribute status = (XmlAttribute)searchResponse.Attributes.GetNamedItem("status"); 
if (status != null) 
{ 
    string statusReturn = status.Value; 
    return statusReturn; 
} 
else 
{ 
    return "value is null"; 
} 

任何幫助你可以在狀態值給予讚賞。我一直在xmlattrbute狀態行收到對象引用錯誤。

+0

請縮進你的XML示例,不可能讀出哪一個很難說出你想要提取的內容。 – millimoose 2012-07-31 21:43:51

回答

3

您的XML文檔包含名稱空間。在執行任何XPath查詢之前,您需要考慮XML文檔中的命名空間。

請參閱this question瞭解如何在C#代碼中添加這些命名空間並在您的XPath中引用它們;或見this question如何使用通配符匹配

0

試試這個關於大小(雖然你的情況,那將變得一團糟,因爲你必須做的每一個元素名稱。):

 const string ValueIsNull = "value is null"; 
     string returnResponse; 

     using (var response = (HttpWebResponse)request.GetResponse()) 
     { 
      if (response == null) 
      { 
       return ValueIsNull; 
      } 

      using (var responseStream = response.GetResponseStream()) 
      { 
       if (responseStream == null) 
       { 
        return ValueIsNull; 
       } 

       using (var reader = new StreamReader(responseStream)) 
       { 
        returnResponse = reader.ReadToEnd(); 
       } 
      } 
     } 

     var doc = new XmlDocument(); 

     doc.LoadXml(returnResponse); 

     var namespaces = new XmlNamespaceManager(doc.NameTable); 

     namespaces.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); 
     namespaces.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); 
     namespaces.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
     namespaces.AddNamespace("ns1", "urn:oasis:names:tc:SPML:2:0:search"); 
     namespaces.AddNamespace("ns2", "urn:oasis:names:tc:SPML:2:0"); 
     namespaces.AddNamespace("ns3", "urn:oasis:names:tc:SPML:2:0"); 
     namespaces.AddNamespace("ns4", "urn:oasis:names:tc:DSML:2:0:core"); 

     XmlNode root = doc.DocumentElement; 

     if (root == null) 
     { 
      return ValueIsNull; 
     } 

     var searchResponse = root.SelectSingleNode("/soapenv:Envelope/soapenv:Body/processRequestResponse/parameters/ns1:searchResponse", namespaces); 

     if ((searchResponse == null) || (searchResponse.Attributes == null)) 
     { 
      return ValueIsNull; 
     } 

     var status = (XmlAttribute)searchResponse.Attributes.GetNamedItem("status"); 

     return status == null ? ValueIsNull : status.Value;