如何将大型xml文件的一部分反序列化为c#类?我已经阅读了一些关于如何反序列化xml的帖子和文章,但仍然没有想出我应该编写代码以满足我的需要的方式,所以..我为另一个关于反序列化xml的问题道歉))我有一个很大(50MB)我需要反序列化的xml文件。我使用xsd.exe获取文档的xsd架构,而不是自动生成我放入项目中的c#类文件。我想从此xml文件中获取一些(不是全部)数据并将其放入我的sql数据库中。这是文件层次结构(经过简化,xsd非常大):publicclassyml_catalogShopOffersOffer{//这里是我想要获取的所有数据(属性)))}这是我的代码:第一种方法:yml_catalogShopOffersOffer目录;varserializer=newXmlSerializer(typeof(yml_catalogShopOffersOffer));varreader=newStreamReader(@"C:div_kid.xml");catalog=(yml_catalogShopOffersOffer)serializer.Deserialize(reader);//异常发生reader.Close();我得到InvalidOperationException:ThereisanerrorintheXML(3,2)document第二种方法:XmlSerializerser=newXmlSerializer(typeof(yml_catalogShopOffersOffer));yml_catalogShopOffersOffer结果;使用(XmlReaderreader=XmlReader.Create(@"C:div_kid.xml")){result=(yml_catalogShopOffersOffer)ser.Deserialize);//发生异常}InvalidOperationException:XML(0,0)文档中的错误第三:我试图反序列化整个文档:XmlSerializerser=newXmlSerializer(typeof(yml_catalog));//异常发生yml_catalog结果;使用(XmlReaderreader=XmlReader.Create(@"C:div_kid.xml")){result=(yml_catalog)ser.Deserialize(reader);我得到以下信息:错误CS0030:无法将类型“yml_catalogShopOffersOffersOffers[]”转换为“yml_catalogShopOffersOffers”。错误CS0029:“yml_catalogShopOffersOffersOffers”类型的隐式转换。那么,如何修复(或覆盖)代码以避免异常?编辑:当我写的时候:XDocumentdoc=XDocument.Parse(@"C:div_kid.xml");发生XmlException:根级别的未经授权数据,字符串1,位置1这是xml文件字符串的第一个字符:编辑2:xml文件的简短示例:OZON.ru??????"???????????????????????"http://www.ozon.ru/basecategoryblablabla//这里有所有类别//其他优惠PS我已经接受了答案(这是完美的)。但现在我需要找到“基本类别”对于每个使用categoryId的报价。数据是分层的,基类是没有“parentId”属性的。所以,我写了一个递归方法来查找“基类”,但它永远不会完成。似乎算法不是很快))这是我的代码:(在main()方法中)vardoc=XDocument.Load(@"C:div_kid.xml");varoffers=doc.Descendants("shop").元素(“优惠”)。元素(“优惠”);foreach(varofferinoffers.Take(2)){varcategory=GetCategory(categoryId,doc);//这里是其他代码}辅助方法:publicstaticstringGetCategory(intcategoryId,XDocumentdocument){vartempId=categoryId;varcategories=document.Descendants("shop").Elements("categories").Elements("类别");foreach(varcategoryincategories){if(category.Attribute("id").ToString()==categoryId.ToString()){if(category.Attributes().Count()==1){返回类别。字符串();}tempId=Convert.ToInt32(category.Attribute("parentId"));}}返回GetCategory(tempId,文档);我可以在这种情况下使用递归吗?如果没有,我怎样才能找到“基类”?试试LINQtoXML。XElement结果=XElement.Load(@"C:div_kid.xml");在LINQ中查询很棒,但一开始肯定有点奇怪。您可以使用SQL语法或使用lambda表达式从文档中选择节点。然后创建包含您感兴趣的数据的匿名对象(或使用现有类)。最好在实际操作中查看它。根据您的示例XML和代码,这是一个具体示例:varelement=XElement.Load(@"C:div_kid.xml");varshopsQuery=fromshopinelement.Descendants("shop")选择新{Name=(string)shop.Descendants("name").FirstOrDefault(),Company=(string)shop.Descendants("company").FirstOrDefault(),Categories=fromcategoryinshop.Descendants("category")选择新的{Id=category.Attribute("id").Value,Parent=category.Attribute("parentId").Value,Name=category.Value},Offers=fromofferinshop.Descendants("offer")selectnew{Price=(string)offer.Descendants("price").FirstOrDefault(),Picture=(string)offer.Descendants("picture").FirstOrDefault()}};foreach(varshopinshopsQuery){Console.WriteLine(shop.Name);Console.WriteLine(shop.Company);foreach(varcategoryinshop.Categories){Console.WriteLine(category.Name);Console.WriteLine(category.Id);}foreach(varofferinshop.Offers){Console.WriteLine(offer.Price);Console.WriteLine(offer.Picture);}}作为额外的:这是从平面类别元素反序列化类别树的方法你需要一个合适的类来保存它们,因为子列表必须有一个类型:classCategory{publicintId{get;放;公共诠释?ParentId{得到;放;}publicListChildren{get;放;}publicIEnumerableDescendants{get{return(fromchildinChildrenselectchild.Descendants).SelectMany(x=>x).Concat(newCategory[]{this});}}}创建一个列表:varcategories=(fromcategoryinelement.Descendants("category")orderbyint.Parse(category.Attribute("id").Value)selectnewCategory(){Id=int.Parse(category.Attribute("id").Value),ParentId=category.Attribute("parentId")==null?nullasint?:int.Parse(category.Attribute("parentId").Value),Children=新列表()}).Distinct().ToList();然后将它们组织成一棵树(从平面列表中借用层次结构到层次结构):varlookup=categories.ToLookup(cat=>cat.ParentId);foreach(varcategoryincategories){category.Children=lookup[category.Id].ToList();}varrootCategories=lookup[null].ToList();找到包含该Category的根:以上是C#学习教程:如何只将A部分反序列化为c#种类?分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——varroot=(fromcatinrootCategorieswherecat.Descendants.Contains(theCategory)selectcat).FirstOrDefault();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
