S
sherifffruitfly
Hi all,
I'm trying to distill all of the info from google searches into what I
need, with partial success. In truth, the whole xmlNode, Document,
Element, etc group of classes & methods is going over my head - lol!
The structure of the xml file I'm trying to append to is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- stuff -->
<!-- more stuff -->
<Scenarios attr="0">
<Scenario num="0">
<TimeSeries name="cdj" >
<Observation date="01/01/2001" value="1000"/>
a whole bunch more observations
</TimeSeries>
</Scenario>
</Scenarios>
I'm trying to code up a routine that adds another Observation to the
list. Here's what I've got:
public void WriteHistory(HistoryType histype, double val)
{
try
{
XmlDocument cdjDoc = new XmlDocument();
XmlNode parentNode;
cdjDoc.LoadXml(monthlyPath + filename);
parentNode = cdjDoc.DocumentElement;
XmlNode newElement = cdjDoc.CreateNode(XmlNodeType.Element,
"Observation", null);
cdjDoc.AppendChild(newElement);
XmlAttribute date = cdjDoc.CreateAttribute("date");
date.InnerText = DateTime.Now.ToString("MM/dd/yyyy");
newElement.Attributes.Append(date);
XmlAttribute rate = cdjDoc.CreateAttribute("value");
rate.InnerText = val.ToString();
newElement.Attributes.Append(rate);
XmlTextWriter writer = new XmlTextWriter(monthlyPath +
filename,null);
cdjDoc.Save(writer);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Screwed!");
}
This doesn't work - throws an exception with message:
The data at the root level is invalid. Line 1, position 1.
I believe the problem is that I'm not "finding" the proper spot in the
xml structure to AppendChild my new element to. Can someone help me
with this? (Or any other problem, if I'm mistaken in my diagnosis?)
Thanks for any guidance,
cdj
I'm trying to distill all of the info from google searches into what I
need, with partial success. In truth, the whole xmlNode, Document,
Element, etc group of classes & methods is going over my head - lol!
The structure of the xml file I'm trying to append to is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- stuff -->
<!-- more stuff -->
<Scenarios attr="0">
<Scenario num="0">
<TimeSeries name="cdj" >
<Observation date="01/01/2001" value="1000"/>
a whole bunch more observations
</TimeSeries>
</Scenario>
</Scenarios>
I'm trying to code up a routine that adds another Observation to the
list. Here's what I've got:
public void WriteHistory(HistoryType histype, double val)
{
try
{
XmlDocument cdjDoc = new XmlDocument();
XmlNode parentNode;
cdjDoc.LoadXml(monthlyPath + filename);
parentNode = cdjDoc.DocumentElement;
XmlNode newElement = cdjDoc.CreateNode(XmlNodeType.Element,
"Observation", null);
cdjDoc.AppendChild(newElement);
XmlAttribute date = cdjDoc.CreateAttribute("date");
date.InnerText = DateTime.Now.ToString("MM/dd/yyyy");
newElement.Attributes.Append(date);
XmlAttribute rate = cdjDoc.CreateAttribute("value");
rate.InnerText = val.ToString();
newElement.Attributes.Append(rate);
XmlTextWriter writer = new XmlTextWriter(monthlyPath +
filename,null);
cdjDoc.Save(writer);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Screwed!");
}
This doesn't work - throws an exception with message:
The data at the root level is invalid. Line 1, position 1.
I believe the problem is that I'm not "finding" the proper spot in the
xml structure to AppendChild my new element to. Can someone help me
with this? (Or any other problem, if I'm mistaken in my diagnosis?)
Thanks for any guidance,
cdj