简体   繁体   English

xmlns=''> 不是预期的。 - DeserializeXml to object 时 XML 文档 (2, 2) 中存在错误

[英]xmlns=''> was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object

my xml file is我的 xml 文件是

<ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
  <customerSessionId>0ABAAACE-8D64-3191-4732-8476D6E963C6</customerSessionId>
  <numberOfRoomsRequested>1</numberOfRoomsRequested>
  <moreResultsAvailable>true</moreResultsAvailable>
</ns2:HotelListResponse>

and this is my object type这是我的对象类型

[Serializable]
    [XmlRoot("HotelListResponse")]
    public class HotelListResponse
    {
        [XmlElement("customerSessionId")]
        public string customerSessionId { get; set; }
        [XmlElement("numberOfRoomsRequested")]
        public int numberOfRoomsRequested { get; set; }
        [XmlElement("moreResultsAvailable")]
        public bool moreResultsAvailable { get; set; }
    }

And I use this to deserlize xml我用它来反序列化 xml

 public static object DeserializeXml(string xmlData, Type type)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            StringReader reader = new StringReader(xmlData);
            object obj = xmlSerializer.Deserialize(reader);

            return obj;
        }

But I am getting error xmlns=''> was not expected.但我收到错误 xmlns=''> 是意料之中的。 - There is an error in XML document (2, 2) - XML 文档中存在错误 (2, 2)

Got it need to fix class得到它需要修复类

[Serializable, XmlRoot(ElementName = "HotelListResponse", Namespace = "http://v3.hotel.wsapi.ean.com/")]
    public class HotelListResponse
    {
        [XmlElement(ElementName = "customerSessionId", Namespace = "")]
        public string customerSessionId { get; set; }
        [XmlElement(ElementName = "numberOfRoomsRequested", Namespace = "")]
        public int numberOfRoomsRequested { get; set; }
        [XmlElement(ElementName = "moreResultsAvailable", Namespace = "")]
        public bool moreResultsAvailable { get; set; }
    }

Your elements have no namespace prefix, so it is expecting a default namespace.您的元素没有命名空间前缀,因此它需要一个默认命名空间。

If someone is sending you XML like that, ask them to fix it.如果有人向您发送这样的 XML,请要求他们修复它。

Edit: You may be able to explicitly specify the expected namespace with the XmlElementAttribute .编辑:您可以使用XmlElementAttribute显式指定预期的命名空间。

Well, it's 7 years after this question was asked, and thankfully not so many people are still using XML.嗯,这个问题已经问了 7 年了,幸好没有多少人还在使用 XML。 However, if you're using Visual Studio and you have some XML that you need to deserialize, and you can't find the right combination of attributes to decorate the classes with, you can use the 'Edit -> Paste XML as classes' option, which will create the correct set of attributes for you.但是,如果您使用的是 Visual Studio 并且您有一些需要反序列化的 XML,并且您找不到用于装饰类的正确属性组合,则可以使用“编辑 -> 将 XML 粘贴为类”选项,它将为您创建正确的属性集。

Just create a new class in VS, delete the actual class part within the namespace in the newly created file, copy your full XML to the clipboard, and then use the paste option to create the class hierarchy needed for the XML, including all of the correct XML Attributes.只需在 VS 中创建一个新类,删除新创建文件中命名空间内的实际类部分,将完整的 XML 复制到剪贴板,然后使用粘贴选项创建 XML 所需的类层次结构,包括所有正确的 XML 属性。

This saved me a lot of trial and error with getting the correct combination of XmlRoot and XmlType attributes.这为我获得 XmlRoot 和 XmlType 属性的正确组合节省了大量的试验和错误。

Here's the complete generated code for the example XML in the original question:这是原始问题中示例 XML 的完整生成代码:

namespace Test
{

    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://v3.hotel.wsapi.ean.com/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://v3.hotel.wsapi.ean.com/", IsNullable = false)]
    public partial class HotelListResponse
    {

        private string customerSessionIdField;

        private byte numberOfRoomsRequestedField;

        private bool moreResultsAvailableField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public string customerSessionId
        {
            get
            {
                return this.customerSessionIdField;
            }
            set
            {
                this.customerSessionIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public byte numberOfRoomsRequested
        {
            get
            {
                return this.numberOfRoomsRequestedField;
            }
            set
            {
                this.numberOfRoomsRequestedField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public bool moreResultsAvailable
        {
            get
            {
                return this.moreResultsAvailableField;
            }
            set
            {
                this.moreResultsAvailableField = value;
            }
        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 xmlns=&#39;&#39;&gt; 不是预期的。 - XML 文档中存在错误 (2, 2) - xmlns=''> was not expected. - There is an error in XML document (2, 2) 将Xml反序列化为对象时出错-{ <string xmlns='http://tempuri.org/'> 没想到。} - Error Deserializing Xml to Object - {<string xmlns='http://tempuri.org/'> was not expected.} xml文档中有错误(2 2)xmlns =“ - there is an error in xml document (2 2) xmlns='' was not expected C#从xml反序列化到对象( <result xmlns=''> 没想到。) - C# Deserilize from xml to object (<result xmlns=''> was not expected.) {”<user xmlns=''> 不是预期的。} 反序列化 Twitter XML - {"<user xmlns=''> was not expected.} Deserializing Twitter XML 将 Xml 反序列化为对象时出错 - 不需要 xmlns=&#39;&#39; - Error Deserializing Xml to Object - xmlns='' was not expected 反序列化 XML 时出错:{&quot;<query xmlns=''> 没想到。&quot;} - Errors de-serializing XML: {"<query xmlns=''> was not expected."} 将XML响应转换为C#对象-错误(不期望xmlns =&#39;&#39;&gt;) - Converting XML response to C# object - Error ( xmlns=''> was not expected ) 反序列化为 object 时出现“xmlns=''&gt; 不是预期的”(在某些计算机上) - "xmlns=''> was not expected" while deserializing to object (on some computers) 无法反序列化XML文档(内部例外: - Unable to Deserialize XML document (Inner Exception: <ArrayOfString xmlns ="> was not expected)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM