Loading XML results in list longer than XML
I'm struggling with this, am not sure what to search for, and I can't really post a code snippet as it's part of a larger project.
I have a List<int> that is part of a class that gets saved/serialized to XML.
When I load the XML, it's creating a List multiple times longer than what is in the XML.
I've created breakpoints and watch the List, and during XML loading, the List goes from an exception (as expected because default class value is a new empty list), to 152 once loaded, while the XML/List only contains 16 elements.
The strange thing that I've noticed, is the additional values are identical to default values that I had previously set in the Class.
A bit of background is that the List was originally an Array containing 8 values, which I then expanded to 16 values. However for some reason the second 8 values weren't getting loaded, which is why I converted it to a List. However I'm now getting far many more elements available, rather than too few.
I have been through all references to the List, and the only one that writes values, is my code for storing values into the List, which results in only 16 values being stored to the XML.
However when the XML is loaded, it adds the old default 16 values to the start of the List, so the saved values get overwritten.
I'm currently wondering if this is some kind of caching bug in VS, as I can't find any reason for the old default values to even be known (they are all commented out in the code)
20 Replies
For reference, this is what's stored in the XML
Yet as soon as the XML has been loaded, this is what is loaded
XML 🤮
How are you deserializing it?
I want to keep the values user readable
JSON is user-readable
Just the basic LoadXML
Ah, I forgot how garbage the XML APIs are...
Uh... are you maybe appending to the file instead of overwriting it...?
I don't really have any better ideas to be honest
It's got me totally stumped. XML only contains the 16 values, yet LoadXML somehow converts it to 152, combined with the old default values
And it also seems strange that when I extended the old Array, it didn't load the additional values
Yeah, gonna be hard to tell without seeing actual code
If it's no secret, you could link a Github repo
It is on GitHub, but it uses an external .Net along with various other files, so isn't a straight forward compile
I have just tried renaming the List, and now no values get loaded from the XML
No need to compile, just wanna see the code
Just figured it out
I changed the code from this particular class being saved as part of a 'parent' class, to this class being saved separately, so you can have different versions of this class.
However, when the 'parent' class is loaded, this class also gets loaded as part of the parent, and in some of the code I was still referencing the parent , so the values have been getting added and saved as part of the parent class.
Pretty sure all of it could've been avoided by just using json lol
It's also made me realise I've not tidied up the parent class, and there is another list being saved there, that shouldn't be.
I can't remember why I chose XML for this, as it's been an ongoing project for several years now, but how many changes would be needed to use JSON instead?
XML API is notoriously shit, non-generic, all the worst traits of ancient versions of C#
JSON is simple
That's it
Does JSON let you serialize dictionaries?
Sure does
Angius
REPL Result: Success
Result: string
Compile: 480.107ms | Execution: 77.578ms | React with ❌ to remove this embed.
Excellent. I know this project contains a couple basic custom classes, just to allow me to save what could be done in dictionaries
I'll add converting to JSON to the todo list
Anyway, thanks for the help. Sometimes you just need a distraction to find the real problem.
:Ok: