Sunday, 11 January 2015

Create xml node with innertext from textbox using data binding



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


No comments:

Post a Comment