C# 如何解决rest服务返回的xml反序列化问题?

C# 如何解决rest服务返回的xml反序列化问题?,c#,xml,deserialization,C#,Xml,Deserialization,当我试图从web客户机反序列化rest服务响应的xml时,出现以下错误 {System.InvalidOperationException:XML文档中存在错误 (1,2)。-->System.InvalidOperationException:未启用 预计在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRoot.Read6_Root() ---内部异常堆栈跟踪结束---位于System.Xml.Seri

当我试图从web客户机反序列化rest服务响应的xml时,出现以下错误

{System.InvalidOperationException:XML文档中存在错误 (1,2)。-->System.InvalidOperationException:未启用 预计在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRoot.Read6_Root() ---内部异常堆栈跟踪结束---位于System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader、字符串编码样式、对象事件) System.Xml.Serialization.XmlSerializer.Deserialize(流)
在XmlParser.MainPage XmlParser.MainPage.c_DisplayClass6.b_5(对象s, OpenReadCompletedEventArgs)位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 在System.Net.WebClient.OpenReadOperationCompleted(对象arg)}

服务返回的Xml是

<?xml version="1.0"?>
<ns0:Response xmlns:ns0="urn:ae:gov:testwebsite:uniqueness:genericcontentsrs:1">
    <GenericContents>
        <ModuleId>1296</ModuleId>
        <Title>Getting around</Title>
        <Description>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum., <a target="_blank" href="http://www.google.com">google.com</a>, provides useful information. People often rely on landmarks to give directions.<br /> <br />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. gmk&rsquo;s Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</Description>
        <BuildingId>0</BuildingId>
        <GeoCoordinateX/>
        <GeoCoordinateY/>
    </GenericContents>
</ns0:Response>
这是我用来反序列化的代码

private Root DeserializeXmlData(System.IO.Stream stream)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Root));
            Root result = (Root)ser.Deserialize(stream);
            return result;
        }

ns0
是xml文档中未声明的前缀,很可能导致错误

如果有必要,则需要将其包装在信封中,信封元素中包含
xmlns:n0=“http:somenamespace”
属性。

元素的名称是
Response
,而不是
ns0:Response
。名称空间前缀在形式上不是名称的一部分。以下两段xml在信息上完全相同:

<a:b xmlns:a="urn:damiens_namespace"/>


(再次查看您的代码,我不确定
Root
类的用途。我希望
XmlRoot()
应用于
nsResponse
类,并将该类传递给
XmlSerializer

此问题是由于rest服务返回的Xml文件中的空节点值造成的。在Xml文件中有两个空节点值GeoCoordinateX和GeoCoordinateY

这个问题通过如下更改反序列化类来解决

[XmlRoot(ElementName = "ns0:Response",)]
    public class Root
    {
        [XmlElement(ElementName = "ns0:Response")]
        public nsResponse _nsResponse { get; set; }  
    }

    public class nsResponse
    {
        [XmlElement(ElementName = "GenericContents")]
        public GenericContents _GenericContents { get; set; } 
    }

    public class GenericContents
    {
        private string moduleIdField;

        private string titleField;

        private string descriptionField;

        private string buildingIdField;

        private string geoCoordinateXField;

        private string geoCoordinateYField;

        public string ModuleId
        {
            get
            {
                return (!string.IsNullOrEmpty(moduleIdField)) ? moduleIdField.ToString() : "";
            }
            set
            {
                this.moduleIdField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string Title
        {
            get
            {
                return (!string.IsNullOrEmpty(titleField)) ? titleField.ToString() : "";
            }
            set
            {
                this.titleField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string Description
        {
            get
            {
                return (!string.IsNullOrEmpty(descriptionField)) ? descriptionField.ToString() : "";
            }
            set
            {
                this.descriptionField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string BuildingId
        {
            get
            {
                return (!string.IsNullOrEmpty(buildingIdField)) ? buildingIdField.ToString() : "";
            }
            set
            {
                this.buildingIdField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string GeoCoordinateX
        {
            get
            {
                return (!string.IsNullOrEmpty(geoCoordinateXField)) ? geoCoordinateXField.ToString() : "";
            }
            set
            {
                this.geoCoordinateXField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string GeoCoordinateY
        {
            get
            {
                return (!string.IsNullOrEmpty(geoCoordinateYField)) ? geoCoordinateYField.ToString() : "";
            }
            set
            {
                this.geoCoordinateYField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }
    }
[XmlRoot(ElementName=“ns0:Response”,)]
公共类根
{
[xmlement(ElementName=“ns0:Response”)]
公共nsResponse_nsResponse{get;set;}
}
公共类nsrresponse
{
[XmlElement(ElementName=“GenericContents”)]
公共GenericContents _GenericContents{get;set;}
}
公共类泛型内容
{
私有字符串moduleId字段;
私有字符串标题字段;
私有字符串描述符字段;
私有字符串构建域;
专用字符串地理坐标字段;
私有串地理坐标场;
公共字符串模块ID
{
得到
{
返回(!string.IsNullOrEmpty(moduleIdField))?moduleIdField.ToString():“”;
}
设置
{
this.moduleIdField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
/// 
公共字符串标题
{
得到
{
返回(!string.IsNullOrEmpty(titleField))?titleField.ToString():“”;
}
设置
{
this.titleField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
/// 
公共字符串描述
{
得到
{
返回(!string.IsNullOrEmpty(descriptionField))?descriptionField.ToString():“”;
}
设置
{
this.descriptionField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
/// 
公共字符串BuildingId
{
得到
{
返回(!string.IsNullOrEmpty(buildingIdField))?buildingIdField.ToString():“”;
}
设置
{
this.buildingIdField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
/// 
公共字符串地理坐标
{
得到
{
返回(!string.IsNullOrEmpty(geoCoordinateXField))?geoCoordinateXField.ToString():“”;
}
设置
{
this.geoCoordinateXField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
/// 
公共字符串地理坐标
{
得到
{
返回(!string.IsNullOrEmpty(geoCoordinateYField))?geoCoordinateYField.ToString():“”;
}
设置
{
this.geocordinateyField=!string.IsNullOrEmpty(值)?(值).ToString():“”;
}
}
}

向我们展示您用于反序列化的代码xml@Alberto我已添加代码。请查找我的编辑。我不同意。From:“名称空间前缀(除非是xml或xmlns)必须在使用前缀的元素的开始标记或祖先元素的名称空间声明属性中声明”您是正确的。我对提供的xml和代码做了更多的尝试,但出于某种原因,男孩哦男孩不喜欢那个xml。@不信者damien谢谢,但现在我得到了
类型的异常“System.invalidoOperationException”出现在System.xml.Serialization.ni.dll中,但没有在user
中处理。请帮助我解决这个问题。
<y:b xmlns:y="urn:damiens_namespace"/>
[XmlRoot(ElementName = "Response",
         Namespace="urn:ae:gov:testwebsite:uniqueness:genericcontentsrs:1")]
    public class Root
    {
[XmlRoot(ElementName = "ns0:Response",)]
    public class Root
    {
        [XmlElement(ElementName = "ns0:Response")]
        public nsResponse _nsResponse { get; set; }  
    }

    public class nsResponse
    {
        [XmlElement(ElementName = "GenericContents")]
        public GenericContents _GenericContents { get; set; } 
    }

    public class GenericContents
    {
        private string moduleIdField;

        private string titleField;

        private string descriptionField;

        private string buildingIdField;

        private string geoCoordinateXField;

        private string geoCoordinateYField;

        public string ModuleId
        {
            get
            {
                return (!string.IsNullOrEmpty(moduleIdField)) ? moduleIdField.ToString() : "";
            }
            set
            {
                this.moduleIdField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string Title
        {
            get
            {
                return (!string.IsNullOrEmpty(titleField)) ? titleField.ToString() : "";
            }
            set
            {
                this.titleField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string Description
        {
            get
            {
                return (!string.IsNullOrEmpty(descriptionField)) ? descriptionField.ToString() : "";
            }
            set
            {
                this.descriptionField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string BuildingId
        {
            get
            {
                return (!string.IsNullOrEmpty(buildingIdField)) ? buildingIdField.ToString() : "";
            }
            set
            {
                this.buildingIdField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string GeoCoordinateX
        {
            get
            {
                return (!string.IsNullOrEmpty(geoCoordinateXField)) ? geoCoordinateXField.ToString() : "";
            }
            set
            {
                this.geoCoordinateXField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }

        /// <remarks/>
        public string GeoCoordinateY
        {
            get
            {
                return (!string.IsNullOrEmpty(geoCoordinateYField)) ? geoCoordinateYField.ToString() : "";
            }
            set
            {
                this.geoCoordinateYField = !string.IsNullOrEmpty(value) ? (value).ToString() : "";
            }
        }
    }