简体   繁体   English

XML反序列化-不需要xmlns

[英]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.

相关问题 XML 反序列化 System.InvalidOperationException”<objects xmlns=""> 没想到</objects> - XML deserialization System.InvalidOperationException” the <Objects xmlns=''> was not expected C#中的XML反序列化错误-InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 没想到 - XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected xml文档中有错误(2 2)xmlns =“ - there is an error in xml document (2 2) xmlns='' was not expected XML反序列化 <rss xmlns=''> 没想到 - XML Deserialize <rss xmlns=''> was not expected 在 C# 反序列化期间,出现 System.InvalidOperationException: &lt; xmlns=''&gt; 不是预期的 - During C# deserialization, getting System.InvalidOperationException: < xmlns=''> was not expected XML反序列化错误:不应预期为xxxxx - XML Deserialization error: xxxxx was not expected 反序列化xml。 xmlns =&#39;http // someaddress&#39;意外 - Deserialize xml. xmlns='http//someaddress' was not expected xmlns=&#39;&#39;&gt; 不是预期的。 - XML 文档中存在错误 (2, 2) - xmlns=''> was not expected. - There is an error in XML document (2, 2) 将 Xml 反序列化为对象时出错 - 不需要 xmlns=&#39;&#39; - Error Deserializing Xml to Object - xmlns='' was not expected 将 Xml 反序列化为列表<T> - xmlns=&#39;&#39; 不是预期的 - Deserializing Xml to List<T> - xmlns='' was not expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM