Help writing XML innertext to nodes
I have a method that creates(loads) an xml file. It sets all of my innertext nodes to some default values. If you change the selected item in the drowdown it is supposed to change the value that is in the innertext of whatever node it is corresponding to in the XML file. How do I go from loading my XML file, to changing the values within my innertext?
Sam HobbsPosted Jul 28, 2010, 12:11 PM
DamodarPosted Jul 28, 2010, 10:19 AM
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load ("Filename");
XmlNode node = xmldoc.DocumentElement;
foreach(XmlNode testConfig in node.ChildNodes)
{
foreach (XmlNode TbleItem in testConfig.ChildNodes)
{
//Write the code to find out the thing you want to change
if(TbleItm.innertext == "Name of the tag whose innertext u want to change")
TbleItm.innertext = "The value";
}
}
Hope this will make the thing easy for you.
CherRon McLemorePosted Jul 21, 2010, 10:51 AM
XmlNode newNode = rootNode.SelectSingleNode("//upperRow/item[0]");
newNode.InnerText = "firstItem";
Using this syntax I can change the innertext of only the first item. (Since I only wanted to change certain singular nodes)
Lakitha MunasinghePosted Jul 16, 2010, 6:39 AM
// This is how you can load your XML file...
private void LoadXmlDocument()
{
try
{
String filePath = @"C:/abc.xml";
XmlTextReader reader = new XmlTextReader(filePath);
doc.Load(reader);
}
catch (FileNotFoundException fno)
{
throw;
}
}
To change the value of the innerText... (Ex- Change the value "First" to "Last")
XmlNode xmlNode = doc.DocumentElement;
if (xmlNode != null)
{
XmlNodeList nodes = xmlNode.ChildNodes;
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].ChildNodes[0].HasChildNodes)
{
for (int j = 0; j < nodes[i].ChildNodes[0].ChildNodes.Count; j++)
{
nodes[i].ChildNodes[0].ChildNodes[j].InnerText = dropdown.SelectedItem.ToString();
}
}
}
}
Hope you will get good results using this.
Good Luck!!!
Regards,
Lakitha Munasinghe