-
July 26th, 2006, 03:23 PM
#1
Reading + Writing XML
Im pretty n00bish so im sure it's just a dumb mistake I did. But I can't read the xml and then write something on it.
static string LoadXML(String TheParent, String TheChild)
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"SP.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("XMLDoc/" + TheParent);
System.Xml.XmlNode SPChild = SPParent.Item(0).SelectSingleNode(TheChild);
return SPChild.InnerText;
}
public void WriteXML(String TheParent, String TheChild, String TheValue)
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"SP.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("XMLDoc/" + TheParent);
System.Xml.XmlNode SPChild = SPParent.Item(0).SelectSingleNode(TheChild);
System.Xml.XmlTextWriter Writer = new System.Xml.XmlTextWriter(TheValue, System.Text.UTF8Encoding.UTF8);
SP.WriteTo(Writer);
Writer.Flush();
}
private void button1_Click(object sender, EventArgs e)
{
int TestMe = Int32.Parse(LoadXML("Main", "Subjects")) + 1;
WriteXML("Main", "Subjects", TestMe.ToString());
Label1.Text = LoadXML("Main", "Subjects");
}
There are no errors, I can build it and perform the button click just fine. The XML looks like this:
<XMLDoc>
<Main>
<Subjects>3</Subjects>
</Main>
</XMLDoc>
When I call the button click, Label1.Text says there is still 3 for the subject. I also tried:
private void button1_Click(object sender, EventArgs e)
{
int TestMe = 15;
WriteXML("Main", "Subjects", TestMe.ToString());
Label1.Text = LoadXML("Main", "Subjects");
}
And it still shows 3 in Label1.text.
-
July 28th, 2006, 01:37 AM
#2
Re: Reading + Writing XML
-
July 28th, 2006, 01:57 AM
#3
Re: Reading + Writing XML
Ok I played around with it some more.. Came out to:
static string LoadXML(String TheParent, String TheChild)
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"SP.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("XMLDoc/" + TheParent);
System.Xml.XmlNode SPChild = SPParent.Item(0).SelectSingleNode(TheChild);
return SPChild.InnerText;
}
public void WriteXML(String TheParent, String TheChild, String TheValue)
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"SP.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("XMLDoc/" + TheParent);
System.Xml.XmlNode SPChild = SPParent.Item(0).SelectSingleNode(TheChild);
System.Xml.XmlTextWriter Writer = new System.Xml.XmlTextWriter("SP.xml", System.Text.UTF8Encoding.UTF8);
Writer.WriteStartElement("XMLDoc");
Writer.WriteStartElement(TheParent);
Writer.WriteStartElement(TheChild, TheValue);
SPChild.WriteTo(Writer);
Writer.Flush();
}
private void button1_Click(object sender, EventArgs e)
{
int TestMe = 15;// Int32.Parse(LoadXML("Main", "Subjects")) + 1;
WriteXML("Main", "Subjects", TestMe.ToString());
QsStr.Text = LoadXML("Main", "Subjects");
}
XML is in the debug and it looks like:
<XMLDoc>
<Main>
<Subjects>3</Subjects>
</Main>
</XMLDoc>
So now it's highlighting the line:
System.Xml.XmlTextWriter Writer = new System.Xml.XmlTextWriter("SP.xml", System.Text.UTF8Encoding.UTF8);
And it says that, "The process cannot access the file ... because it is being used by another process." ... Any ideas?
-
July 28th, 2006, 02:03 AM
#4
Re: Reading + Writing XML
have you tried using XmlDocument and XmlElement?
to load ( i find this easier to use )
Code:
XmlDocument doc = new XmlDocument();
doc.Load(filename);
Last edited by ideru; July 28th, 2006 at 02:22 AM.
-
July 28th, 2006, 02:22 AM
#5
Re: Reading + Writing XML
i modified your code a bit
Code:
private string LoadXML(String TheParent, String TheChild )
{
XmlDocument doc = new XmlDocument();
doc.Load(@"SP.xml");
XmlNode node = doc.DocumentElement;
string data = "";
if( node.LocalName == TheParent )
{
IEnumerator rootIter = node.GetEnumerator();
while( rootIter.MoveNext() )
{
XmlNode currentNode = (XmlNode)rootIter.Current;
data = currentNode[TheChild ].InnerText;
break;
}
return data;
}
}
-
July 28th, 2006, 04:59 PM
#6
Re: Reading + Writing XML
Aright so I changed my code to match yours, and tried changing the WriteXML() to sorta match the LoadXML() method. Came out to:
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;
}
}
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;// Int32.Parse(LoadXML("Main", "Subjects")) + 1;
WriteXML("Main", "Subjects", TestMe.ToString());
QsStr.Text = LoadXML("Main", "Subjects");
}
...Same thing. The LoadXML() works fine, and i'm even checking the .xml itself, and it isn't changing. Theres no error but it won't write the file. I also checked the debug folder and there is no extra file in there that it may have written to. =( Any ideas or suggestions?
-
July 29th, 2006, 04:58 AM
#7
Re: Reading + Writing XML
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.
-
July 29th, 2006, 09:30 AM
#8
Re: Reading + Writing XML
sorry i forgot to write, the writing code
Code:
private void SetElement(XmlElement settingElement, string path, string value)
{
if( settingElement[path].InnerText == value)
{
settingElement[path].InnerText = value;
}
else
{
XmlDocument doc = settingElement.OwnerDocument;
XmlElement newElement = doc.CreateElement(path)
newElement[path].InnerText = value
}
}
Code:
private void WriteXml(string parent, string child, string value)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.DocumentElement;
IEnumerator rootIter = node.GetEnumerator();
while( rootIter .MoveNext() )
{
XmlNode curnode = (XmlNode)rootIter .Current;
if( curnode.LocalName == child )
{
SetElement(curnode, child, value);
break;
}
}
doc.Save(filename)
}
basically this is how i usually do it. Basically you need also to add some checking in the setting area if ever there is an exception.
// i have not compiled the code above, so there might be some syntax error
// hope it works..
Last edited by ideru; July 29th, 2006 at 09:34 AM.
-
July 31st, 2006, 10:40 AM
#9
Re: Reading + Writing XML
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?
-
August 3rd, 2006, 12:41 PM
#10
Re: Reading + Writing XML
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|