What is the best way to update the XML node value in C #?

My function is iterating through each node of the instance XMLDocument

. It checks if the current node name is in the search list. If so, it applies the appropriate check for the value of the current node.

When the validation method indicates that a value has been changed, I want to replace the value in the original document with the updated value.

I think the simplest way to achieve this might be writing to XMLTextWriter

where I process each node in the original XMLDocument

, either writing out the original, or the modified node and value if needed. This method will rely on determining if the current node has any children or is a standalone node.

Is there a better way to update the values ​​in the original document? I need to end up with a complete XMLDocument

but updated node values ​​where needed.

Thanks in advance.

+1


source to share


2 answers


Can't you modify the existing nodes (which were already in the correct structure and in the XMLDocument and then re-serialize the XMLDocument? If the nodes are simple text locks, then

.InnerText

      



Property

is the one you want.

+3


source


I know I always come back to this, but it looks like an example where clever use of Application Templates and ExtensionObjects in XSLT would be effective.



This means XMLDocument is optimized for modification, so if you go with a clean programmatic solution, I would modify the object straight away rather than create a new Writer.

+1


source







All Articles