How to find and update a record in a recursive model?
I have the following classes:
I'm using it in this code:
I thought I was sharing the reference, but that wasn't right. So either I figure out how to properly share the node through a pointer or I need to search through _node for the proper node to select or update.
Any suggestions? Any articles to help me understand this better?
9 Replies
Background info: these nodes represent a XML file. It might be easier to rely on XDocument instead, but I want to learn more about recursive design.
What is it you're actually trying to do here? The code isn't super clear about what the problem you're facing is.
My recommendation would be to rely on
XDocument
as you get all the tree traversal built-in via Descendants
.Making a web app to edit xml. I have a component that shows the data of the selected node
That component allows editing and returns the changed node in OnNodeUpdated
Hmmmmm, unfortunately doesn't really give me the info I'd need to understand your precise challenge with this here, anything I'd be likely to suggest would be too subjective.
Ya I was worried about that. I can share the project if that helps, or show the razor html
You wrote
I thought I was sharing the reference, but that wasn't rightWhat do you mean by this? I'm also having trouble understanding your actual problem here. Are there exceptions? Unexpected behaviors? $details please
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
(because btw, you're not traversing your tree in the methods you shared so any help we could give would be guesswork at best)
If you're having issues with traversing the tree and seeing e.g. unexpected descendants, I'd suggest sharing that specific bit of code.
Your tree data structure seems fine, although there is imo no point using a record for this.
My issue was finding and editing a node especially if two or more nodes had the same name without any id. Clint made a good point and I've refactored the code
I wrapped the XDocument stuff because I wanted flexibility on how to access the info: if I change my mind on which package or method to do something, I'll have one place to change it, not dozens
This is what solved my issue: