1

I think this is a very basic question, but I can't find answer to it.

I got an XML file which loaded into a textblock of a treeview using hierarchical data template:

<HierarchicalDataTemplate.Triggers>
  <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
    <Setter TargetName="textBlock1" Property="Text" Value="{Binding Path=Name}"/>
  </DataTrigger>
</HierarchicalDataTemplate.Triggers>

The c# code to open an XML:

XmlDocument doc = new XmlDocument();
doc.Load(open.FileName);
XmlDataProvider dp = (XmlDataProvider)this.FindResource("nodes");
dp.Document = doc;
dp.XPath = "*";

When I double click on the textBlock1 a new window appears with a cancel and a save button, and a textbox(what I want to edit) which contains the selected node:

XmlNode selected_xNode = tree.SelectedItem as XmlNode;
openWindow.textBox1.Text = selected_xNode.Name;

This works fine, but I don't know how can I change the selected node name to the textbox.text when I click on the save button?

I want something similar to this:

selected_xNode.Name = textBox1.Text;

1 Answer 1

1

I think you need to open the xml file and write to the specified node and then save it again. You can't just try to change the tree.

XmlDocument doc = new XmlDocument(); 
doc.Load(open.FileName);
  1. First you need to find the element, preferably with LiNq to Xml.

  2. Then write it back again (replacing, the old one).

  3. Save

You have already the XmlNode so it will propably be sufficient to find the node already.

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.