❔ struggling with reading data from xml files
Hello there. I'm struggling with referencing only the subpart nodes inside the relevant bodypart node, instead of either all or none of them.
56 Replies
struggling how?
sorry i'm having a hard time figuring out how to explain it. So second image, ive got a body object with it's constructor using the attributes in the xml file. the body object has a Parts list which i am able to add to successfully.
so the human body in the xml file has the head, chest, gut, arms in it's Parts list. but each of those head, chest etc objects also has a list of sub parts, which is where the eyes, ears etc are supposed to go.
the problem i'm having is I can't figure out how to reference the subparts in the xml file. The code in the second image sees the bodypart, but not any subpart
define "sees the bodypart, but not any subpart"
the body object'Parts list is being filled properly (ex the body has a head, chest, etc), but the head's subpart list is not being added to (the head bodypart object is supposed to have eyes, ears etc in it's subparts list)
but the head's subpart list is not being added towhy not?
i'm assuming it's because in the second image i posted, i'm not referring to the subparts in the xml file correctly.
depends how you think about it
how you refer to the subparts in the XML depends on 2 things
where you're referring from and what path you refer to
what is
node
?also here's my github because at this point it's probably eaiser https://github.com/Wicked-Cat/StoryRPG
GitHub
GitHub - Wicked-Cat/StoryRPG
Contribute to Wicked-Cat/StoryRPG development by creating an account on GitHub.
in engine/factories/bodyfactory
so
node
is a <Body>
yes
let's say the first one, for the moment
how many
<SubParts>
children does it have?the whole body? technically none
that would by why
node.SelectNodes("SubParts/SubPart")
returns nothing, thenright. I don't know what the correct code would look like
depends what you want to do
i already told you what i want to do
not really
you said that the subparts lists aren't getting populated
what do you want to populate them with?
with the subparts in the xml file. so the head subparts list gets populated with onlythe subparts within the head bodypart tags
so, for starters, you want to be searching for subparts only within the head bodypart
or whatever your current bodypart is
not the whole body
yes. I don't know how to do that
sure you do
how do you search for nodes within a node
I think it would make more sense to me if I saw an example of this being done correctly
it wouldn't look any different than what you have now
you literally have one line to change
so it would look different than what I have now
no, as I said
there's no new syntax
no new method calls
nothing new that you're not doing already
you're just searching on the wrong node
you're searching for head subparts within the entire body
instead of within the head bodypart
i've tried childnode.selectnodes, or node.selectnodes("parts/bodyparts/subparts/subpart"). both of thoses things result in all head subparts from all bodies being applied to each head
gonna have to disagree
is correct
disagree with... my results? because those are my results
the code you have results in this
then you're not running the code you showed me
plus the line I just gave
this is what i'm running
and the first image i posted is the xml file
plus you have my git
that code is correct
start challenging your assumptions
is that the code you're actually running?
are you quite sure you have the correct XML file?
is your output routine correct?
is your output routine actually operating on the right body objects?
define output routine please
whatever is generating that screenshot you posted
it's just a foreach for the output
looks reasonable
looks like the subparts lists aren't getting subparts from other bodies but it's being duplicated a differing amount of times
like, the rat body only has twice the subparts, but the human body has four times
sounds like you're leaking list instances
what does that mean?
you're inadvertently reusing lists you've already populated, instead of creating a new one each time
I kinda suspected that earlier, but I didn't see it
huh. what's the best practice for something like this? should i clear the lists in the foreach? or delete them somehow
no, your goal is to use unique lists each time
i'm struggling hard to understand the minutiae of how ToList copies things, but am I on the right track?
possibly
there isn't really any minutiae,
.ToList()
creates a full duplicate list in memory
at any rate, the XML logic is goodBlazeBin - airdjrksbykn
A tool for sharing your source code with the world!
I don't understand what this is for
this is to verify that the logic you now have for reading the XML is correct
ah alright
do you have any advice for what I should do about the lists? I don't even know where to start to fix this
not a lot
I really don't see the issue
I will suggest you handle this differently
question: what's your intention here, regarding mutating these models?
can parts be added/removed at runtime?
or should that just be part of initialization?
if you need to be able to add/remove parts at any time, what you should do is....
this prevents you from accidentally screwing up your instance management
the
List<T>
is still editable by consumers, but they can't replace the whole instance
nor should they
if parts should only be created during initialization, then you should do....
this clarifies your intent, as the list can now not be edited at all
you can still mutate the individual parts within it, but the list itself is frozen
your initializer would then change to look something like....
so, you create the list, populate it, and then hand it to the new owner
the new owner doesn't even KNOW that it's a List<T>
cause it doesn't care
it only knows that it's an IReadOnlyList<T>
because that's the minimum functionality that it requires
and again
getting rid of set
prevents you from screwing up the instance management
there's a decent chance that removing the set
will immediately reveal the problem, as it'll cause a compiler error to throw at some line that's swapping out the instance where it shouldn'twell, i got rid of the set, no error: ')
I do want to be able to add and remove parts during runtime
then just leave it as a
List<T>
that you set in the constructor
beyond that, you're gonna need to start digging with the debuggerWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.