Ok X_x I'm having problems again. I can't save the document...
I want to do both adding an element and changing one:
I have -
<Ore>
<Copper>
<NumOre>0</NumOre>
</Copper>
<Tin>
<NumOre>0</NumOre>
</Tin>
<Iron>
<NumOre>0</NumOre>
</Iron>
</Ore>
I want -
<Ore>
<Copper>
<NumOre>0</NumOre>
</Copper>
<Tin>
<NumOre>1</NumOre>
<Col1>35,67,82</Col1>
</Tin>
<Iron>
<NumOre>0</NumOre>
</Iron>
</Ore>
The code I have:
static string GetOreNum(String TheChild)
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"Colors.xml");
System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
System.Xml.XmlDocument SP = new System.Xml.XmlDocument();
SP.Load(xr);
System.Xml.XmlNodeList SPParent = SP.SelectNodes("Ore/" + TheChild);
System.Xml.XmlNode SPChild = SPParent.Item(0).SelectSingleNode("NumOre");
return SPChild.InnerText;
}
public void WriteXML(String TheParent, String TheChild, String NChild, String TheValue)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(@"Colors.xml");
System.Xml.XmlNode node = doc.DocumentElement;
AddElement(node[TheChild], NChild, TheValue);
String OreNum = GetOreNum(TheChild);
int NewOreNum = Int32.Parse(OreNum) + 1;
SetElement(node[TheChild], "NumOre", NewOreNum.ToString());
doc.Save(@"Colors.xml");
}
private void AddElement(System.Xml.XmlElement settingElement, string path, string value)
{
System.Xml.XmlDocument doc = settingElement.OwnerDocument;
System.Xml.XmlElement newElement = doc.CreateElement(path);
newElement.InnerText = value;
settingElement.AppendChild(newElement);
}
private void SetElement(System.Xml.XmlElement settingElement, string path, string value)
{
if (settingElement[path].InnerText != value)
settingElement[path].InnerText = value;
}
String Parent = "Ore";
String Child = "Tin";
String OreNum = GetOreNum(Child);
int NewOreNum = Int32.Parse(OreNum) + 1;
Col.Text = "35,67,82";
WriteXML(Parent, Child, "Col" + NewOreNum.ToString(), Col.Text);
I am getting the error on:
doc.Save(@"Colors.xml");
IOException was unhandled
The process cannot access the file "path" because it is being used by another process.
I don't have the file open and no other program is using it. Any ideas? =( I know the write works because I can save it to a different filename and it saves fine and has all the stuff I want, but it won't save to the same filename that it was loaded from.