[英]XML Deserialization - xmlns not expected
/I have an error when I tried to deserialize a xml file: /当我尝试反序列化xml文件时出现错误:
System.InvalidOperationException: There is an error in XML document (2, 2). ---> <MessageIpBNotifGetInventaire xmlns='http://www.idele.fr/XML/Schema/'> was not expected.
The section of my XML that was a problem looks like this: 我的XML出现问题的部分如下所示:
<ns2:MessageIpBNotifGetInventaire xmlns="http://www.fiea.org/types/" xmlns:ns2="http://www.idele.fr/XML/Schema/">
My class is defined like this: 我的课是这样定义的:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.idele.fr/XML/Schema/")]
[System.Xml.Serialization.XmlRootAttribute("MessageIpBNotifGetInventaire", Namespace = "http://www.idele.fr/XML/Schema/", IsNullable = false)]
public partial class MessageIpBNotifGetInventaire {
Here is my deserialization code 这是我的反序列化代码
using (MemoryStream inputStream = new MemoryStream(reponseSpecifique.MessageZip))
{
using (ZipFile zipFile = ZipFile.Read(inputStream))
{
zipFile.Save("D:\\Test.zip");
foreach (ZipEntry zipEntry in zipFile)
{
using (MemoryStream outputStream = new MemoryStream())
{
zipEntry.Extract(outputStream);
outputStream.Position = 0;
using (StreamReader sr = new StreamReader(outputStream, Encoding.GetEncoding("UTF-8")))
{
XmlReader reader = XmlReader.Create(outputStream);
XmlSerializer serializer = new XmlSerializer(typeof(MessageIpBNotifGetInventaire));
MessageIpBNotifGetInventaire messageRetour = (MessageIpBNotifGetInventaire)serializer.Deserialize(reader);
}
}
}
}
}
The problem is that the namespace in your XmlRootAttribute
definition doesn't match that in the XML document itself. 问题在于XmlRootAttribute
定义中的名称空间与XML文档本身中的名称空间不匹配。 One has a /
at the end and the other doesn't. 一个在末尾带有/
,另一个则没有。
You might think, don't those URLs point to the same thing, but actually using URLS as namespaces is just a convention (one I don't follow myself, I prefer using urn:companyname:schemas:appname
or something along those lines), and the strings do have to match exactly. 您可能会认为,这些URL并非指向同一事物,但实际上使用URL作为命名空间只是一种约定(我不跟我学,我更喜欢使用urn:companyname:schemas:appname
或类似的urn:companyname:schemas:appname
) ,并且字符串必须完全匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.