Click here to Skip to main content
13,863,229 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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.
Posted
Updated 11-Feb-19 8: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>
Member 12061698 11-Feb-19 13:11pm
   
That's a cut-and-paste error on my part. Just assume it's there. I was trying to give a shortened version that would fit in the space allowed. The XML in the program is correct.
Maciej Los 11-Feb-19 14:17pm
   
OK. What's your class definition?
Member 12061698 11-Feb-19 14:21pm
   
This is the beginning of the class. The web page won't let me post the full version. I'm wondering if I need to add an attached property like
[Serializable, XmlRoot("Incident")] to the class. ( I tried it and it DIDN'T work).

public class Incident : DomainObject
{
#region Incident Number
private string incidentNumber;
[XmlElement(ElementName = "IncidentNum")]
.
.
.
objects and methods
.
.
.
}
Member 12061698 11-Feb-19 16:35pm
   
I may have stumbled on a way past the problem. Here's the Serialization/Deserialization:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Incident), new XmlRootAttribute { ElementName = "IncidentGet" });
System.IO.TextReader textReader = new System.IO.StringReader(dataXml);

incident = (Incident)xmlSerializer.Deserialize(textReader);

When I took out the XmlRootAttribute parameter for the serializer, the process worked.

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Incident));
System.IO.TextReader textReader = new System.IO.StringReader(dataXml);

incident = (Incident)xmlSerializer.Deserialize(textReader);

I still don't understand exactly what was wrong.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

Examples of XML Serialization | Microsoft Docs[^]
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190214.1 | Last Updated 11 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100