【发布时间】:2012-03-15 08:27:49
【问题描述】:
我正在尝试反序列化 XML 流并收到以下错误:
第 1 行第 7 位出错。需要命名空间“http://schemas.datacontract.org/2004/07/Veracross”中的元素“auth”。遇到名称为“auth”、命名空间“”的“元素”。
我正在反序列化的 XML 流如下所示:
<auth>
<status>success</status>
<username>jsmith</username>
<person_pk>1234</person_pk>
<security_roles>Parent</security_roles>
</auth>
我的代码:
[DataContract(Name = "auth")]
public class Authorization
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "username")]
public string UserName { get; set; }
[DataMember(Name = "security_roles")]
public string SecurityRoles { get; set; }
}
// Some code here receiving the XML and storing in a string (xmlData)
DataContractSerializer serializer = new DataContractSerializer(typeof(Authorization));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData));
Authorization Auth = (Authorization)serializer.ReadObject(stream);
我认为它对准系统 XML 文件(没有标题信息)不满意,但我无法控制它。它从 RESTful 服务中使用。
【问题讨论】:
-
看起来它需要一个命名空间为“schemas.datacontract.org/2004/07/Veracross”的元素身份验证,但它发现没有任何命名空间的身份验证。我不确定错误来自哪里,您可能需要将命名空间添加到解析器或类似的东西。
-
谢谢,我尝试用 [DataContract (Namespace = "schemas.datacontract.org/2004/07/Veracross", Name = "auth"] 装饰 auth 类定义,但错误是一样的。
-
我在命名空间前加上“http://”,但它没有以分号结尾 - 所以我的评论搞砸了。
标签: xml serialization deserialization