我开发WCF-client。我的客户应该验证传入的消息。
其中一条消息的结构如下:
<?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“具有来自命名空间"http://www.w3.org/2001/XMLSchema”的类型"dateTime“。
此响应在包含XmlElement数组的结构中反序列化。
但是我有一个问题:在消息被反序列化后,我收到了相应的变量,包含了所有的字段节点,我不能确定前缀"xsd“的值,也就是说,如果我在回复中接受任何与字段节点对应的XMLElement类型的元素,并调用element.GetNamespaceOfPrefix("xsd"),结果就会得到空字符串。
如何在反序列化后保存前缀的定义?
请帮助我克服这个问题。
发布于 2013-10-11 20:12:47
为了影响名称空间/前缀,您需要使用XmlSerializerNamespaces。
以下代码提供了粗略的参考:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("prefixHere", "http://namespace.here/");
XmlSerializer tempSerializer = new XmlSerializer(messageObject.GetType());
tempSerializer.Serialize(Console.Out, messageObject, namespaces); 致以敬意,
https://stackoverflow.com/questions/19296612
复制相似问题