# Issue with loading XML

So recently i've been changing my XML files that i'm using in-game, to add in a bit more data to the files but i've also changed the way in which i load & save files, by now using the XDocument API. *Previous Thread/Topic: Xna, writing XML </Asset> end element

So now that i'm all done, i've come to trying out the new files in my new project and i have a strange error: There was an error while deserializing intermediate XML. Input string was not in a correct format.

Oddly though, this at line 13: http://pastebin.com/jMtNwV8N

In all honesty i don't know what to say. And to what exactly the problem is, i'm having a slight hunch that it could be something to-do with the way the files are saved: http://pastebin.com/Q2pJye5B

Problem is that my initial thinking right now is that it might have to be something to-do with the '\t' or '\n' literals, but then there's these literals in the actual data lines i.e lines 11 & 12 so it wouldn't all of a sudden chuck me an error at line 13

Guess i'm just a bit mystified by this and to what the problem might be?

Any help greatly appreciated :)!

Edit: Well just to test it out then, i removed all the '\t' & '\n' from the saving code. And re-tried it then, unfortunately i'm still getting the same error :/ Same place, with the error occurring at the ending element of Rect.

• Where's the code? Jan 11, 2012 at 22:51
• I just had a hunch. Are you perhaps adding this XML to the XNA content pipeline and trying to build it? If you are, you shouldn't be doing that. You should mark the "Build Action" property on the XML file to "Content" (and copy to output folder) instead of "Compile". Jan 11, 2012 at 23:02
• Are you referring to the loading code that i'm using? I'll add that in now Jan 11, 2012 at 23:03
• Damn it! David you are correct, seems i forgot about that :/. Jan 11, 2012 at 23:04
• And yes, i have a custom content reader to read the XML files into the game. Jan 11, 2012 at 23:07

## 2 Answers

The word deserializing on your error message tipped me off - that wasn't supposed to appear in a normal scenario. That means it's a problem with the XNA content pipeline.

This happens because you were trying to build the XML through XNA's content pipeline. The solution is as simple as changing its Build Action property on the file from Compile to Content, and toggling the Copy to Output Directory property to true.

My old advice still applies though:

I don't think you should be adding whitespace to your data as a way to indent and make your XML look better, since they're not part of the data you're trying to store. This will force you to trim it when you read the data back, which is unnecessary in my opinion.

I think you should get rid of the tabs altogether, and possibly choose another character to act as the line separator (e.g. instead of using "\n" use a normal " " space).

Edit

i think i might know what it is that is causing the issue. For some reason it seems, compiling the XML into the game, it doesn't like the ',' thats is separating the values. Changing that it works :O. Unsure why its so fussy?

I am guessing that maybe your content writer is still doing a split by " " and then writing it in that way to the stream, but since you're now separating your values by "," it's not written correctly. So when deserializing, the ReadObject method is expecting a list of rectangles, but finds something else and fails.

Add your content reader and writer to your post if you need help figuring that out, but it really should be just a matter of keeping both with sync with each other and with the file format.

Edit 2

By the way, this doesn't look right:

anim.Rect = new List<Rectangle>();
anim.Rect = (List<Rectangle>)input.ReadObject<List<Rectangle>>();


You probably only need the second line. You're creating a new list and then overwriting it immediately. Not that it doesn't work, but is wasteful.

• Hummh, actually i don't think it's that. Basically what i'm doing is converting from an older project and trying to re-new some elements of it, checking the older project, everything was set to compile. Jan 11, 2012 at 23:20
• Loading code: pastebin.com/xSSXH14e Its been awhile since i've actually went and re-did this stuff see, i remember this code as being one of the first things i did on this old project. Jan 11, 2012 at 23:22
• i think i might know what it is that is causing the issue. For some reason it seems, compiling the XML into the game, it doesn't like the ',' thats is separating the values. Changing that it works :O. Unsure why its so fussy? Jan 11, 2012 at 23:31
• It looks like your custom content reader doesn't expect the commas? Your first example didn't have commas there. Also, you never posted that code. Jan 11, 2012 at 23:47
• I am guessing it's because ContentReader.ReadObject ((List<Rectangle>)input.ReadObject<List<Rectangle>>();) probably expects your data to be formatted stored in a certain way? Show yous your content writer too, the problem is probably there (not serializing correctly to xnb). Jan 11, 2012 at 23:55

I don't know the XDocument API as I do not practice C#, but from my own experience with others languages you are probably right when you are suspecting the '\t' and '\n'.

I tried to load and parse your xml file with tinyxml in C++, but I did not get any error. However, there is two solutions that come to my mind. You could try the following options:

1. Use the CDATA element as the text inside a CDATA section will be ignored by the parser.
2. Use a trim function to remove the white spaces and line breaks.
• Well just to test it out then, i removed all the '\t' & '\n' from the saving code. And re-tried it then, unfortunately i'm still getting the same error :/ Jan 11, 2012 at 22:57