I'm trying to modify existing XML data. For example, I have an xml file in the following format:
I also have a form with 1 combobox (e.g vin.text) and 4 text boxes (e.g. color.text, engine.text, type.text, and price.text), which corresponds to the xml nodes. I'm attempting to write a method that will update the xml data, if it corresponds to the vin number. So, if I want to change the price on vin 1255588 from 11000 to 09999, how could I accomplish that?
heres my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlTextReader reader = new XmlTextReader(@"C:\test.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
//Select the vin node
XmlNode oldvin;
XmlElement root = doc.DocumentElement;
oldvin = root.SelectSingleNode("/cars/car/[vin='" + vin.Text + "']");
XmlElement newCd = doc.CreateElement("car");
newCd.SetAttribute("vin", vin.Text);
newCd.InnerXml = "
"
"
"
"
root.ReplaceChild(newCd, oldvin);
//save the output back to the xml file
doc.Save(@"C:\test.xml");
this.DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
Console.WriteLine ("Exception: {0}", ex.ToString());
this.DialogResult = DialogResult.Cancel;
}
}
}
}
PriyankaPosted Mar 22, 2010, 7:01 AM
You can fing the solution to your problem here.
Hope this helps you.!!!!!