我正在创建一个简单的控制台应用程序来获取来自云 Web 服务的响应。我能够以 xml 格式获取响应有效负载,但是我发现很难读取 XML 标签并从标签中获取值。
以下是我收到的回复,
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:Action>http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService//FlowActionsService/getFlowTaskInstanceStatusResponse</wsa:Action>
<wsa:MessageID>urn:uuid:2a2436b3-c018-4ef7-93b6-bd8b82a82041</wsa:MessageID>
</env:Header>
<env:Body>
<ns0:getFlowTaskInstanceStatusResponse xmlns:ns0="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/">
<result xmlns="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/">COMPLETED</result>
</ns0:getFlowTaskInstanceStatusResponse>
</env:Body>
</env:Envelope>
我正在尝试阅读带有结果标签的文本。有人可以帮助我处理 C# 中的代码吗? 如果需要更多信息,请告诉我。
示例 C# 类:
[XmlRoot(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Header {
[XmlElement(ElementName="Action", Namespace="http://www.w3.org/2005/08/addressing")]
public string Action { get; set; }
[XmlElement(ElementName="MessageID", Namespace="http://www.w3.org/2005/08/addressing")]
public string MessageID { get; set; }
}
[XmlRoot(ElementName="result", Namespace="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/")]
public class Result {
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="getFlowTaskInstanceStatusResponse", Namespace="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/")]
public class GetFlowTaskInstanceStatusResponse {
[XmlElement(ElementName="result", Namespace="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/")]
public Result Result { get; set; }
[XmlAttribute(AttributeName="ns0", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns0 { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="getFlowTaskInstanceStatusResponse", Namespace="http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/")]
public GetFlowTaskInstanceStatusResponse GetFlowTaskInstanceStatusResponse { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Header Header { get; set; }
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="env", Namespace="http://www.w3.org/2000/xmlns/")]
public string Env { get; set; }
[XmlAttribute(AttributeName="wsa", Namespace="http://www.w3.org/2000/xmlns/")]
public string Wsa { get; set; }
}
并反序列化它:
string sampleXml;
sampleXml.DeSerializeXMLToObject<Header>();
感谢您的帮助。我在网上找到了这个解决方案,它解决了我获取带有命名空间的标签值的情况
XDocument xDoc = XDocument.Parse(response);
XNamespace rst = XNamespace.Get("http://xmlns.oracle.com/apps/hcm/processFlows/core/flowActionsService/types/");
var rslt = xDoc.Root.Descendants(rst + "result").First();
flowStatus = rslt.Value;