本文介绍了当xmlserializer.deserialize遇到未映射到对象的节点/标记时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试从TextReader反序列化XML并收到此错误: 发生'System.InvalidOperationException'类型的异常在Microsoft.GeneratedCode中并且在托管/本地边界之前未处理 附加信息:< Incident xmlns =''>没想到。 错误发生在最后一行: string dataXml =( string )dbDataReader [ DataXML]; XmlSerializer xmlSerializer = new XmlSerializer( typeof (突发事件), new XmlRootAttribute {ElementName = IncidentGet}); System.IO.TextReader textReader = new System.IO.StringReader(dataXml); incident =(Incident)xmlSerializer.Deserialize(textReader); dataXML中有几个未映射到的标签任何对象。解串器只是忽略它们还是导致错误?或者错误来自其他来源? < 事件 > < incidentnum > 20180323905 < OccurredOn / > < reporton > 2019-02-01 < reportingofficer > TEST,TEST < location > < recnum > 102953131 < house > 4471 < street > TESTSTREETNAME2 PK < CrossStreet / > < streetaddr > 4471 TESTSTREETNAME2 PK < ; city > TESTCITY < 州 > TN < zip > 37013 < X / > < Y / > < 纬度 / > < 经度 / > < 县 > 019 < 已验证 / > < 区域 > SOUTH < RPA / > < 区域 / > < countrycode > USA < 违规行为 > < 冒犯 > < 代码 > 120 < / code > < description > ROBBERY - STREET -WEAPON < 进攻 > < code > < 描述 > MATTEROFRECORD < / code > < 代码 > < / code > < 代码 > < 受害者 > < 受害者 > < involvednum > 1 < 种族 > B < 性别 > F < dob > 1988-07- 13 < 名称 > TESTNAME2,TESTNAME3 < masterrecnum > 71931912 < victimoffenses > < victimoffense > < attackcode > 0003 < NibCode / > < offensename > 记录事项 < victimnum > 1 < linkofflrecnum > 60036199 < linkoffloffenserec > 13808415 < linkofflrecnum > 6296009 < linkvictimrecnum > 71931912 < 关系 > < 关系 > < victimnumber > 1 < suspectnumber > 1 < relation > 17 < victimname > TESTNAME3 TESTNAME2 < suspectname > TESTNAME4 TESTNAME5 < linkincsusrecnum > 6356248 < 嫌疑人 > < suspect > < suspecttype > 已知 < involvednum > 1 < race > B < 性别 > M < dob > 07/16/1987 < 年龄 / > < 名称 > TESTNAME5,TESTNAME4 < linkincsusrecnum > ; 6356248 < source_rec > 60036198 < callsforservice > < callforservice > < location > < recnum > 102524782 < House / > < 街道 / > < crossstreet > 17TH AV N < streetaddr > 1600 TESTSTREETNAME ST < city > NORTH < 状态 / > < Zip / > < X / > < Y / > < 纬度 / > < 经度 / > < 县 / > < 已验证 / > < 区域 > NORTH < rpa > 4475 < 区域 > 613N < CountryCode / > < callreceived > 20180411 05:07 < tencode > 41P 我尝试了什么: 我没有尝试过多少。我在团队框架内工作,不能在不影响整个团队的情况下更改代码。我只是想确定错误的确切来源和性质。解决方案 注意使用错误(事件)处理程序。 XML示例序列化| Microsoft Docs [ ^ ] I am trying to deserialize XML from a TextReader and getting this error: exception of type 'System.InvalidOperationException' occurred in Microsoft.GeneratedCode and wasn't handled before a managed/native boundaryAdditional information: <Incident xmlns=''> was not expected.The error occurs on the last line:string dataXml = (string)dbDataReader["DataXML"];XmlSerializer xmlSerializer = new XmlSerializer(typeof(Incident), new XmlRootAttribute { ElementName = "IncidentGet" });System.IO.TextReader textReader = new System.IO.StringReader(dataXml);incident = (Incident)xmlSerializer.Deserialize(textReader);There are several tags in dataXML which are not mapped to any object at all. Does the deserializer just ignore them or do they cause the error? Or does the error come from some other source?<incident><incidentnum>20180323905<OccurredOn /><reportedon>2019-02-01<reportingofficer>TEST,TEST<location><recnum>102953131<house>4471<street>TESTSTREETNAME2 PK<CrossStreet /><streetaddr>4471 TESTSTREETNAME2 PK<city>TESTCITY<state>TN<zip>37013<X /><Y /><Latitude /><Longitude /><county>019<Validated /><precinct>SOUTH<RPA /><Zone /><countrycode>USA<offenses><offense><code>120</code><description>ROBBERY - STREET-WEAPON<offense><code><description>MATTEROFRECORD</code><code></code><code><victims><victim><involvementnum>1<race>B<sex>F<dob>1988-07-13<name>TESTNAME2,TESTNAME3<masterrecnum>71931912<victimoffenses><victimoffense><offensecode>0003<NibCode /><offensename>MATTER OF RECORD<victimnum>1<linkofflrecnum>60036199<linkoffloffenserec>13808415<linkofflrecnum>6296009<linkvictimrecnum>71931912<relationships><relationship><victimnumber>1<suspectnumber>1<relation>17<victimname>TESTNAME3 TESTNAME2<suspectname>TESTNAME4 TESTNAME5<linkincsusrecnum>6356248<suspects><suspect><suspecttype>KNOWN<involvementnum>1<race>B<sex>M<dob>07/16/1987<Age /><name>TESTNAME5, TESTNAME4<linkincsusrecnum>6356248<source_rec>60036198<callsforservice><callforservice><location><recnum>102524782<House /><Street /><crossstreet>17TH AV N<streetaddr>1600 TESTSTREETNAME ST<city>NORTH<State /><Zip /><X /><Y /><Latitude /><Longitude /><County /><Validated /><precinct>NORTH<rpa>4475<zone>613N<CountryCode /><callreceived>20180411 05:07<tencode>41P"What I have tried:I haven't tried much. I'm working within a team framework and can't change code without affecting the entire team. I'm just trying to nail down the exact source and nature of the error. 解决方案 Note the use of the "error (event) handlers".Examples of XML Serialization | Microsoft Docs[^] 这篇关于当xmlserializer.deserialize遇到未映射到对象的节点/标记时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 19:35