我一直在搜索这一段时间,找不到解决方案。我有一个从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状态行收到对象引用错误。
请缩进你的XML示例,不可能读出哪一个很难说出你想要提取的内容。 – millimoose 2012-07-31 21:43:51