AuthenticateUserOutput
我们从 WCF Web 服务返回类的实例。
所以我们有这个方法:
public override AuthenticateUserOutput AuthenticateUser(AuthenticateUserInput AuthenticateUserInput)
AuthencateUserOutput 是自动生成的:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.5420")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="xxxx")]
public partial class AuthenticateUserOutput : WS2MethodOutput
{
private bool authenticationResultField;
private UserContext userContextField;
/// <remarks/>
public bool AuthenticationResult
{
get
{
return this.authenticationResultField;
}
set
{
this.authenticationResultField = value;
}
}
/// <remarks/>
public UserContext UserContext
{
get
{
return this.userContextField;
}
set
{
this.userContextField = value;
}
}
}
我们需要能够反序列化为AuthenticateUserOutput
,但它不起作用。
作为测试,我实例化了一个AuthenticateUserOutput
,序列化它并尝试反序列化它。
它因 InvalidOperationException 而失败。
这是序列化的 XML:
<?xml version="1.0" encoding="utf-16"?>
<AuthenticateUserOutput xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="xxxx">
<Response xmlns="xxxx">
<IsValid>true</IsValid>
<Success>true</Success>
</Response>
<AuthenticationResult xmlns="xxxx">true</AuthenticationResult>
<UserContext xmlns="xxxx">
</UserContext>
</AuthenticateUserOutput>
下面是序列化和反序列化代码:
public static string ToXml(this object input)
{
string output;
XmlSerializer serializer = new XmlSerializer(input.GetType());
StringBuilder sb = new StringBuilder();
using (TextWriter textWriter = new StringWriter(sb))
{
serializer.Serialize(textWriter, input);
}
output = sb.ToString();
return output;
}
和:
public static T FromXml<T>(this string input)
{
T output;
XmlSerializer serializer = new XmlSerializer(typeof(T));
output = (T)serializer.Deserialize(new StringReader(input));
return output;
}
异常的具体细节:
System.InvalidOperationException occurred
Message="<AuthenticateUserOutput xmlns='xxxx'> was not expected."
Source="qkxd8dd-"
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderAuthenticateUserOutput.Read31_AuthenticateUserOutput()
InnerException:
所以它不能反序列化它自己的序列化 Xml。
有人能看出为什么吗?
谢谢,
J1M。
更新:这是测试代码:
AuthenticateUserOutput test = new AuthenticateUserOutput();
test.AuthenticationResult = true;
test.Response = new ResponseType();
test.Response.Exception = null;
test.Response.IsValid = true;
test.Response.Success = true;
test.Response.ValidationErrors = null;
test.UserContext = new UserContext();
string serializedXml = test.ToXml();
AuthenticateUserOutput deserializedString = serializedXml.FromXml<AuthenticateUserOutput>();
UPDATE2 : 多亏了 Mark Gravell,反序列化器才能工作
好的,我忘了我已经添加了这个来让命名空间在反序列化时正确出现:
public partial class WS2MethodInput
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces _xmlns;
/// <summary>
/// Constructor for WS2MethodInput that sets up default namespaces
/// </summary>
public WS2MethodInput()
{
_xmlns = new XmlSerializerNamespaces();
_xmlns.Add("", "xxxx");
}
}
有了这个,序列化的 Xml 就像在这条消息的顶部一样。没有它,反序列化程序可以工作,但 AuthenticateUserOutput 缺少命名空间:
<?xml version="1.0" encoding="utf-16"?>
<AuthenticateUserOutput xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="xxxx">
<Response xmlns="xxxx">
<IsValid>true</IsValid>
<Success>true</Success>
</Response>
<AuthenticationResult xmlns="xxxx">true</AuthenticationResult>
<UserContext xmlns="xxxx">
</UserContext>
</AuthenticateUserOutput>
注意xmlns="xxxx"
末尾的AuthenticateUserOutput
问题是,现在我不能在没有其他代码的情况下使用该 Xml:
1)将其加载到 XDocument 中并添加命名空间并在我需要对其进行反序列化时将其删除 2)对正则表达式的字符串替换或其他内容执行相同操作
我真的不喜欢这两个。事实上,这太可怕了!8X