【问题标题】:Difficulty reading SOAP response due to special chars由于特殊字符而难以阅读 SOAP 响应
【发布时间】:2017-11-27 12:57:41
【问题描述】:

我一直在使用 C# 处理 SOAP 请求/响应。我得到了请求和响应格式应该如何。我已经成功创建了一个发送请求的[WebMethod],我可以从中检索XML 响应。问题是我得到的响应是这样的

<soapenv:Header>
<v1:ResultStatus xmlns:v1="http://group.com/contract/vho/header/v1">
<bf:Timestamp xmlns:bf="http://docs.oasis-open.org/wsrf/bf-2">27-Nov-17 1:42:24 PM</bf:Timestamp>
<bf:ErrorCode xmlns:bf="http://docs.oasis-open.org/wsrf/bf-2">OK</bf:ErrorCode>
<bf:Description xmlns:bf="http://docs.oasis-open.org/wsrf/bf-2">Integration Id cannot be retrieved for this user</bf:Description>
<v11:Message xmlns:v11="http://group.com/schema/common/v1">FAILURE</v11:Message>
</v1:ResultStatus>
</soapenv:Header>

<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v1:CheckAuthenticationCredentialVBMResponse xmlns:v1="http://group.com/schema/vbm/identity/authentication-credential/v1">
<v1:AuthenticationCredentialVBO actionCode="ADD">
<v11:IDs xmlns:v11="http://group.com/schema/common/v1">
<v11:ID schemeName="Authentication Credential ID">5535</v11:ID>
</v11:IDs>
</v1:CheckAuthenticationCredentialVBMResponse>
</SOAP-ENV:Body>

当我尝试使用XmlDocument xmldoc.GetElementsByTagName("bf:ErrorCode");XmlDocument xmldoc.GetElementsByTagName("v11:Message"); 读取它时,它会将: 视为特殊字符,因此无法继续执行,抛出此异常:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

我试图找到一些关于此的文档,但我似乎找不到类似的东西。有人可以帮忙吗?

如果需要更多信息,请发表评论。

【问题讨论】:

  • 我也陷入了这个陷阱,SOAP 不是 xml,非常相似但显然不遵循其规则,因此无法使用 xml 解析器进行解析,您需要使用 SoapClient 类
  • 您可能希望添加更多响应消息以显示已定义命名空间“v1”、“v11”和“bf”。了解“xmldoc”的数据类型也会有所帮助,以便有人可以帮助向您展示处理命名空间的正确 API。
  • @Michael 好的,我会对此进行研究。如果您有与此相关的任何链接,请分享。
  • @Nathan 我将使用非常短的正文版本来编辑帖子,并声明 xmldoc。谢谢你的建议。
  • @Noel 在 google 中找到它:msdn.microsoft.com/en-us/library/ms819935.aspx

标签: c# xml soap


【解决方案1】:

bf:ErrorCode 是前缀bf 指示的命名空间中的xml 元素ErrorCode。你可以在声明中看到这个前缀代表什么命名空间:

xmlns:bf="http://docs.oasis-open.org/wsrf/bf-2" 

所以bf:ErrorCode 是命名空间http://docs.oasis-open.org/wsrf/bf-2 中的元素ErrorCode。请注意,前缀名称(“bf”)无关紧要,重要的是命名空间本身(“http://docs.oasis-open.org/wsrf/bf-2”)。出于这个原因,您不应该通过命名空间前缀搜索元素,因为它可能随时更改而不会更改 xml 文档的含义。您应该只按命名空间搜索。

有了这些知识,你可以得到这样的元素:

string targetNamespace = "http://docs.oasis-open.org/wsrf/bf-2";
var elements = doc.GetElementsByTagName("ErrorCode", targetNamespace);

请注意,GetElementsByTagName 的文档建议不要使用此方法,而是使用 SelectNodes。您可以像这样对SelectNodes 执行相同的操作:

var doc = new XmlDocument();
doc.LoadXml(xml);
var nsManager = new XmlNamespaceManager(doc.NameTable);
// note that prefix doesn't _need_ to be "bf" 
// (though it could be "bf" if you wish)
nsManager.AddNamespace("anyPrefixHere", "http://docs.oasis-open.org/wsrf/bf-2");
// use the same prefix here you used in `AddNamespace` call above
var elements = doc.SelectNodes("//anyPrefixHere:ErrorCode", nsManager);

【讨论】:

  • 很好的答案!完全有效。我将切换到 SelectNodes.. 似乎很容易管理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多