I've been trying to apply TextBox.Text values to my existing XML file nodes, I've tried many ways but it doesn't seem to pick up on it.
My code:
private void btnAddId_Click(object sender, RoutedEventArgs e)
{
if (tbAddId.Text == "")
{
MessageBox.Show("No value was given!");
}
else if (tbAddId.Text == "Add ID")
{
MessageBox.Show("No value was given!");
}
else
{
XmlDocument Xdoc = new XmlDocument();
string xmldoc = (@"// path to my xml file");
Xdoc.Load(xmldoc);
XmlElement elList = (XmlElement)Xdoc.SelectSingleNode("/filter/filter_item");
if (elList != null)
{
XmlNode node = Xdoc.SelectSingleNode("filter_item");
node.InnerText = tbAddId.Text;
elList.AppendChild(node);
}
Xdoc.Save(xmldoc);
}
}
My xml:
<?xml version="1.0"?>
<root>
<filter>
<!-- Copy filter-item and put the order-id in as the value to skip it-->
<filter_item>
</filter_item>
</filter>
</root>
Does anyone know the best way to add TextBox.Text to an existing child node?
The
elListvariable isnull, because you forgot therootnode in the XPath. If there is only onefilter_itemnode, as I guess from your usage ofSelectSingleNode, this should work:This will result in the following XML document: