0

I want to add another list node in product node.

Product.xml file have:

<products>
  <product>
    <list>first</list>
    <list>second</list>
  </product>  
</products>

Code for this is:

<Grid.DataContext>
    <XmlDataProvider x:Name="xmlData" Source="Product.xml" XPath="products/product"/>
</Grid.DataContet>

And in addItem_Click_1:

private void addItem_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {
            string source = xmlData.Source.AbsoluteUri;

            XmlDocument doc = xmlData.Document;

            // Get a handle on the root node.
            XmlNode root = doc.SelectSingleNode("//product");
            XmlNode newitem = doc.CreateElement("item");
            newitem.InnerText = itemTextBox.Text;
            root.AppendChild(newitem);

            // Save the changes.
            xmlData.Document.Save(source);
            MessageBox.Show("Successful");
        }
        catch (Exception d)
        {
            MessageBox.Show(d.Message);
        }
    }

This shows output as:

The operation is not supported for a relative URI.

Product.xml file is:

How can i solve this?Please anyone help

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
miltan
  • 1
  • 3
  • Look at the contents of the source variable. – fejesjoco Jan 11 '15 at 12:40
  • You've already specified the content of `products.xml` at the start of the question - why did you try to add it again later, and then as a comment? *Where* does the exception occur? What's the value of `source`? – Jon Skeet Jan 11 '15 at 12:40
  • I also used this:string source = xmlData.Source.LocalPath; but the same result. – miltan Jan 11 '15 at 13:02
  • possible duplicate of [Using relative URI as XmlDataProvider's Source](http://stackoverflow.com/questions/1059916/using-relative-uri-as-xmldataproviders-source) – dbc Jan 11 '15 at 19:18

0 Answers0