【问题标题】:Lost prefix's definitions after deserialization of WCF messageWCF 消息反序列化后丢失前缀的定义
【发布时间】:2013-10-10 13:07:34
【问题描述】:

我开发 WCF 客户端。我的客户应该验证传入的消息。

其中一条消息具有以下结构:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SOAP-ENV:Header>...</SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <OpDescriptionResponse>
      <Field Name="DateTime" Type="xsd:dateTime">
    </OpDescriptionResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

在这种情况下,客户端应验证:字段“DateTime”的类型为“dateTime”,来自命名空间“http://www.w3.org/2001/XMLSchema”。

此响应在包含 XmlElement 数组的结构中反序列化。

但是我有一个问题:在消息被反序列化并且我收到了包含所有字段节点的相应变量之后,我无法确定前缀“xsd”的值,即,如果我在回复中采用与字段节点对应的 XMLElement 类型的任何元素,并且调用 element.GetNamespaceOfPrefix("xsd") 我得到空字符串。

反序列化后如何保存前缀的定义?

请帮我解决这个问题。

【问题讨论】:

    标签: c# xml wcf


    【解决方案1】:

    为了影响命名空间/前缀,您需要使用 XmlSerializerNamespaces。

    以下代码提供了粗略的参考:

    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
    namespaces.Add("prefixHere", "http://namespace.here/");
    XmlSerializer tempSerializer = new XmlSerializer(messageObject.GetType());
    tempSerializer.Serialize(Console.Out, messageObject, namespaces);  
    

    问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多