C
C#2mo ago
mwc

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)
No description
20 Replies
mwc
mwcOP2mo ago
For reference, this is what's stored in the XML
mwc
mwcOP2mo ago
Yet as soon as the XML has been loaded, this is what is loaded
No description
Angius
Angius2mo ago
XML 🤮 How are you deserializing it?
mwc
mwcOP2mo ago
I want to keep the values user readable
Angius
Angius2mo ago
JSON is user-readable
mwc
mwcOP2mo ago
Just the basic LoadXML
Angius
Angius2mo ago
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
mwc
mwcOP2mo ago
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
Angius
Angius2mo ago
Yeah, gonna be hard to tell without seeing actual code If it's no secret, you could link a Github repo
mwc
mwcOP2mo ago
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
Angius
Angius2mo ago
No need to compile, just wanna see the code
mwc
mwcOP2mo ago
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.
Angius
Angius2mo ago
Pretty sure all of it could've been avoided by just using json lol
mwc
mwcOP2mo ago
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?
Angius
Angius2mo ago
XML API is notoriously shit, non-generic, all the worst traits of ancient versions of C# JSON is simple
var json = JsonSerializer.Serialize(thing);
var thing = JsonSerializer.Deserialize<Thing>(json);
var json = JsonSerializer.Serialize(thing);
var thing = JsonSerializer.Deserialize<Thing>(json);
That's it
mwc
mwcOP2mo ago
Does JSON let you serialize dictionaries?
Angius
Angius2mo ago
Sure does
MODiX
MODiX2mo ago
Angius
REPL Result: Success
using System.Text.Json;

record Foo(Dictionary<int, string> Dict);

var d = new Dictionary<int, string> {
[1] = "one",
[2] = "two",
};
var f = new Foo(d);

JsonSerializer.Serialize(f)
using System.Text.Json;

record Foo(Dictionary<int, string> Dict);

var d = new Dictionary<int, string> {
[1] = "one",
[2] = "two",
};
var f = new Foo(d);

JsonSerializer.Serialize(f)
Result: string
{"Dict":{"1":"one","2":"two"}}
{"Dict":{"1":"one","2":"two"}}
Compile: 480.107ms | Execution: 77.578ms | React with ❌ to remove this embed.
mwc
mwcOP2mo ago
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.
Angius
Angius2mo ago
:Ok:

Did you find this page helpful?