Jump to content
Xtreme .Net Talk

JBudOne

Members
  • Posts

    6
  • Joined

  • Last visited

JBudOne's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. =D Thx guys. Ya I got the textreader idea from a tutorial on basics of xml X_x Thanks =)
  2. 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.
  3. WOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!! IT WORKS!!!!! Omg: PlausiblyDamp, I am forever in your debt. I was having problems with that for so long. Thank you sooooo much. And btw I was using the loop because in another forum I asked about this and he showed me that code, with a loop. I really didn't understand it either. Anyways thanks sooooooo much again =))))))))) You made my day.
  4. Hey, so I played around with it - still having problems. This is what I have: public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; System.Collections.IEnumerator rootIter = node.GetEnumerator(); while( rootIter .MoveNext() ) { System.Xml.XmlElement curNode = (System.Xml.XmlElement)rootIter.Current; if (curNode.LocalName == TheChild) { SetElement(curNode, TheChild, TheValue); break; } } doc.Save(@"SP.xml"); } private void SetElement(System.Xml.XmlElement settingElement, string path, string value) { if( settingElement[path].InnerText == value) { settingElement[path].InnerText = value; } else { System.Xml.XmlDocument doc = settingElement.OwnerDocument; System.Xml.XmlElement newElement = doc.CreateElement(path); newElement[path].InnerText = value; } } WriteXML("Main", "Subjects", "W00000000000T OMG IT WORKS!!!!!!!!!!!!!!!!"); SP.xml <XMLDoc> <Main> <Subjects>3</Subjects> <Questions>18</Questions> <Answered>12</Answered> <Correct>9</Correct> </Main> </XMLDoc> When I try to build it I get an error on: if( settingElement[path].InnerText == value) Object reference not set to an instance of an object. Any ideas or suggestions?
  5. Kk I updated it and came out to this: public static string LoadXML(String TheParent, String TheChild) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; string data = ""; { if (node.LocalName == "XMLDoc") { node.SelectSingleNode(TheParent); System.Collections.IEnumerator rootIter = node.GetEnumerator(); while (rootIter.MoveNext()) { System.Xml.XmlNode currentNode = (System.Xml.XmlNode)rootIter.Current; data = currentNode[TheChild].InnerText; break; } } } return data; } public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; node.SelectSingleNode(TheParent);node.SelectSingleNode(TheChild); System.Xml.XmlTextWriter Writer = new System.Xml.XmlTextWriter("SP.xml", System.Text.UTF8Encoding.UTF8); Writer.WriteString(TheValue); node.WriteTo(Writer); doc.Save(@"SP.xml"); } private void button1_Click(object sender, EventArgs e) { WriteXML("Main", "Subjects", "W00000000000T OMG IT WORKS!!!!!!!!!!!!!!!!"); } And there are no build errors at first. I open it up after I build the prog, and it looks like: W00000000000T OMG IT WORKS!!!!!!!!!!!!!!!!<XMLDoc><Main><Subjects>3</Subjects></Main></XMLDoc> when it used to look like: <XMLDoc> <Main> <Subjects>3</Subjects> </Main> </XMLDoc> Then I build it again and there is an error message: doc.Load(@"SP.xml"); Data at the root level is invalid. Line 1, position 1. Not sure what this means.. Any ideas? I know LoadXML() works, but I can't figure out how to write my message inside the <Subjects> node. Thx.
  6. Ok, so I want to write something into the xml file and then read it. I'm a total n00b and so I'm sure it's something basic. But i've checked and messed around with it for, I guess 3 days now ='( any help would be greatly appreciated. public static string LoadXML(String TheParent, String TheChild) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; string data = ""; { if (node.LocalName == "XMLDoc") { node.SelectSingleNode(TheParent); System.Collections.IEnumerator rootIter = node.GetEnumerator(); while (rootIter.MoveNext()) { System.Xml.XmlNode currentNode = (System.Xml.XmlNode)rootIter.Current; data = currentNode[TheChild].InnerText; break; } } } return data; } public void WriteXML(String TheParent, String TheChild, String TheValue) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(@"SP.xml"); System.Xml.XmlNode node = doc.DocumentElement; node.SelectSingleNode(TheParent); System.Collections.IEnumerator rootIter = node.GetEnumerator(); while (rootIter.MoveNext()) { System.Xml.XmlNode currentNode = (System.Xml.XmlNode)rootIter.Current; currentNode[TheChild].SetAttribute(TheChild, TheValue); break; } private void button1_Click(object sender, EventArgs e) { int TestMe = 15; WriteXML("Main", "Subjects", TestMe.ToString()); Label1.Text = LoadXML("Main", "Subjects"); } The XML looks kind of like: <XMLDoc> <Main> <Subjects>3</Subjects> <Groups>9</Groups> <Qs>18></Qs> </Main> </XMLDoc> So there are no errors when building this, but it shows, '3' in Label1. Any ideas or suggestions are greatly appreciated. Thx.
×
×
  • Create New...