XML序列化和可为空的属性

时间:2019-07-10 11:24:25

标签: c# xml-serialization asp.net-core-2.0

我正在研究需要在我们的数据库中获取表的xml文件的项目。当我在后端使用asp.net内核时,我需要在数据库上查询并序列化为xml并返回响应。根据下面的代码,出现以下错误。

[HttpGet, Route("GetXml")]
        public ActionResult<GenericResponse> GenerateXml()
        {
            var cellData = _repoWrapper.Purchase.FindByCondition(a => a.IsDeleted == false);
            //since servicestack we used in text also have xml serializer so it is necessary to use system.xml .serializer method 
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(cellData.GetType());

            String s= x.ToString();
            return _response;
        }

对于此代码,我收到如下错误: System.InvalidOperationException:'要成为XML可序列化的对象,从IEnumerable继承的类型必须在其继承层次结构的所有级别上都具有Add(System.Object)的实现。 Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'

再次,这样做:

[HttpGet, Route("GetXml")]
        public ActionResult<GenericResponse> GenerateXml()
        {
            var cellData = _repoWrapper.Purchase.FindByCondition(a => a.IsDeleted == false).ToList();
            //since servicestack we used in text also have xml serializer so it is necessary to use system.xml .serializer method 
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(cellData.GetType());

            String s= x.ToString();
            return _response;
        }

我遇到了类似的错误: System.InvalidOperationException:'发生错误,反映了类型'System.Collections.Generic.List' 和: InvalidOperationException:无法序列化类型为System.Nullable'1 [System.Double]的成员'Total'。 XmlAttribute / XmlText不能用于编码复杂类型。

0 个答案:

没有答案