0

I have a TreeView Control on my form which displays an XML file as a tree. And a change button to change the values of the elements in the xml file. I want to do this:

when a user selects a node from the treeview, i want to find that element/value in my xml file. And if the user wants to change its value, he/she types the new value into a textbox and presses the change button. And i want the selected element/value in my xml file to be changed to the new value.

XPath's SelectSingleNode would work with the name of the element. But the thing is there are multiple elements with the same name but different values.

like:

<catalog>
   <book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
   </book>
</catalog>

How to do that? Is it possible to do that with XPathNavigator? I definitely prefer doing that with the help of XPathNavigator.

Any help is appreciated.

4 Answers 4

1

It should be easier to associate the TreeViewNodes with the relevant XmlElement at the point of generating the tree view. You can assign this to the Tag property of each TreeViewNode. When an item is selected, all you have to do is look at the Tag property.

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

Comments

0

Use the XLinq, so the XDocument and XElement, instead of the standard XmlDocument. In such way, your treeview will be automatically updated as any changes made in the XDocument.

When the user selects an item, the SelectedItem property of your treeview exposes the related XElement. If you bind a TextBox to this object, the content will update the element, thus the entire treeview.

Furthermore, any modification to the XDocument structure will be reflected in the treeview.

Cheers

Comments

0

Sorry I doesn't know that :D

Ok, you can have a method that calculate you Selected treeview path:

public string SelectedTreeExplorerPath { get; set; }


private void GetSelectedTreeExplorerPath(TreeViewItem treeItem)
        {
            if (treeItem == null)
                return;
            SelectedTreeExplorerPath = "";
            string temp1 = "";
            string temp2 = "";
            while (true)
            {
                temp1 = treeItem.Header.ToString();
                if (temp1.Contains(@"\"))
                {
                    temp2 = "";
                }
                SelectedTreeExplorerPath = temp1 + temp2 + SelectedTreeExplorerPath;
                if (treeItem.Parent.GetType().Equals(typeof(TreeView)))
                {
                    break;
                }
                treeItem = ((TreeViewItem)treeItem.Parent);
                temp2 = @"\";
            }
        }

In you treeView SelectedItemChanged event you can fire this event:

private void treeViewItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tree = (TreeView)sender;
            TreeViewItem treeItem = ((TreeViewItem)tree.SelectedItem);

            GetSelectedTreeExplorerPath(treeItem);
        }

After you can get the treeItem name you tapped with:

string treeItemName = Path.GetFileName(SelectedTreeExplorerPath);

Now you create:

XPathDocument doc;
XPathNavigator nav;
XPathExpression expr; 
XPathNodeIterator iterator;

Now to search in your XML file inside you treeViewItem_SelectedItemChanged is:

doc = new XPathDocument(FILE_NAME);
nav = doc.CreateNavigator();
// Compile a standard XPath expression
expr = nav.Compile("/catalog/cd/title");
iterator = nav.Select(expr);
while (iterator.MoveNext())
{
    XPathNavigator nav2 = iterator.Current.Clone();
    if (nav2.Value == treeItemName)
    {
        // repleace item as showed in the example project provided as resource below
    }

}

This Article show you how to work with XPath. There demostrate how to update, remove or add a new item. Enter here and download the project to find out the knowledges about how to work XPathNavigator and to change the items.

I hope it helps and it works for you.

Greetings!

1 Comment

Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.
0

If you use a XDocument for handling the XML Data and set the XDocument as the DataContext of the Treeview you can get the XElement corresponding to the selected node with

XElement node = null;
foreach (XElement xe in (theTreeView.DataContext as XDocument).Descendants(System.Xml.Linq.XDocument.Parse(theTreeView.selectedItem).Root.Name.LocalName)){
    if (xe.ToString() == derBaum.SelectedItem.ToString())
    {
        node= xe;
        break;
    }
}

You can manipulate the found node. Changes will be available in the TreeView.

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.