Re: Reading + Writing XML
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?
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);
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;
}
}
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?
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.
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..
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?
Re: Reading + Writing XML