How do I edit a specific node/child of an XML tree?



What is the best way to implement a way to edit a XML file with unknown nodes, subnodes?


For example:



<root>
<subroot>
<name>
<age>
<adress>
<street>
<number>
</adress>
</subroot>
</root>


Each node has a unique id, but every subroot could have more or less nodes than in my example (missing age, having 2 adress) like:



<adress>
.....
</adress>
<adress>
....
</adress>


At the moment I am using the XElement class to write the id and following descendants into a file.



if (element.Attribute("id") != null)
{
result = result.AppendLine(element.Attribute("id").Value);
foreach (var item in element.Descendants())
{
result = result.AppendLine(item.Value);
}
}


I am using C# and want to be able to edit a specific <subroot>s content without knowing how many nodes it could contain. At the end, I have a form where you specify a subroot based on its ID and you are shown dynamically the appropriate amount of text boxes to change them.


No comments:

Post a Comment