|
-
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.
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
|