Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 boundary

Additional information: <Incident xmlns=''> was not expected.


The error occurs on the last line:

C#
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?

XML
<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.
Posted
Updated 11-Feb-19 7:09am
v4
Comments
Maciej Los 11-Feb-19 11:49am    
Can you share xml data and class definition?
We can't help you without knowing details.
Maciej Los 11-Feb-19 12:10pm    
Use Improve question widget! Do not post code in a comment! I'll remove your comments.
Maciej Los 11-Feb-19 12:44pm    
Seems your xml is invalid! Several missing closing tags.
Member 12061698 11-Feb-19 12:53pm    
The site wouldn't let me paste the entire XML. I'm just trying to figure out what
"Additional information: <Incident xmlns=''> was not expected." means. With a web site this uncooperative, it's no wonder I'm having trouble finding an answer.
Maciej Los 11-Feb-19 12:58pm    
I'm talking about this, for example:
<incidentnum>20180323905

it should be:
<incidentnum>20180323905</incidentnum>

1 solution

Note the use of the "error (event) handlers".

Examples of XML Serialization | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900