将新元素附加到xml文件时出现问题... [英] Problem appending a new element to xml file...

查看:62
本文介绍了将新元素附加到xml文件时出现问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试将谷歌搜索中的所有信息提炼到我需要的b $ b,并取得部分成功。实际上,整个xmlNode,Document,

元素等类组和&方法正在我的头上 - 哈哈!


我试图追加的xml文件的结构如下:


<?xml version =" 1.0" encoding =" UTF-8"?>

<! - stuff - >

<! - 更多东西 - >

< Scenarios attr =" 0">

< Scenario num =" 0">

< TimeSeries name =" ; CDJ" >

< Observation date =" 01/01 / 2001" value =" 1000" />


a更多观察结果


< / TimeSeries>

< / Scenario>

< / Scenarios>


我正在尝试编写一个例程,为
添加另一个观察
列表。这就是我所拥有的:


public void WriteHistory(HistoryType histype,double val)

{

尝试

{

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 (WR iter);

}

catch(例外情况)

{

MessageBox.Show(ex.Message, 搞砸了!;

}


这不起作用 - 抛出异常信息:


根级别的数据无效。第1行,第1位。


我认为问题在于我不是找到在我的新元素AppendChild的

xml结构中的适当位置。有人可以帮我吗?b $ b这个? (或者任何其他问题,如果我的诊断错误了吗?)


感谢任何指导,


cdj

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

推荐答案

你自己也很努力。


试试这个(并注意)比大多数代码是XML块使它成为可证明的b $ b;我还在SelectSingleNode

中使用了比可能需要的更奇特的查询...取决于你是否需要选择

*特定的*场景/系列;如果只有一个你可以放弃所有

[@something =''value'']的东西......


使用系统;

使用System.Xml;

class Program {

static void Main()

{

const string xml = @"<?xml version ="" 1.0"" encoding =""" UTF-8""?>


< Scenarios attr ="" 0"">

< Scenario num ="" 0"">

< TimeSeries name ="" cdj"" >

< Observation date ="" 01/01 / 2001"" value =""" 1000"" />

< Observation date ="" 02/02 / 2001"" value =""" 2000"" />

< Observation date ="" 03/03 / 2001"" value ="" 3000"" />

< / TimeSeries>

< / Scenario>

< /场景>" ;;

XmlDocument doc = new XmlDocument();

doc.LoadXml(xml); // TODO:doc.Load(filename);

XmlElement el =(XmlElement)

doc.SelectSingleNode(@" Scenarios [@attr =''0'' ] /场景[@num =''0''] / TimeSeries [@name =''cdj'']");

XmlElement newObservation =

(XmlElement el.AppendChild(doc.CreateElement(" Obse rvation));

newObservation.SetAttribute(" date",

DateTime.Today.ToString(") MM / dd / yyyy));

newObservation.SetAttribute(" value",

DateTime.Today.ToString(" 3000"));


Console.WriteLine(doc.OuterXml); // TODO:doc.Save(filename);

}

}

You''re making it very hard on yourself.

Try this (and note than most of the "code" is the XML block to make it
demonstrable); I have also used a more exotic query in SelectSingleNode
than is probably necessary... depends if you need to select a
*specific* scenario/series; if only one you can drop all the
[@something=''value''] stuff...

using System;
using System.Xml;
class Program {
static void Main()
{
const string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>

<Scenarios attr=""0"">
<Scenario num=""0"">
<TimeSeries name=""cdj"" >
<Observation date=""01/01/2001"" value=""1000""/>
<Observation date=""02/02/2001"" value=""2000""/>
<Observation date=""03/03/2001"" value=""3000""/>
</TimeSeries>
</Scenario>
</Scenarios>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml); //TODO: doc.Load(filename);
XmlElement el = (XmlElement)
doc.SelectSingleNode(@"Scenarios[@attr=''0'']/Scenario[@num=''0'']/TimeSeries[@name=''cdj'']");
XmlElement newObservation =
(XmlElement)el.AppendChild(doc.CreateElement("Obse rvation"));
newObservation.SetAttribute("date",
DateTime.Today.ToString("MM/dd/yyyy"));
newObservation.SetAttribute("value",
DateTime.Today.ToString("3000"));

Console.WriteLine(doc.OuterXml); //TODO: doc.Save(filename);
}
}




Marc Gravell写道:

Marc Gravell wrote:

你自己也很努力。
You''re making it very hard on yourself.



嘿!我在这里试试!


lolol!只是teasin - 谢谢 - 我会给它一个旋转!


cdj

Hey! I''m tryin here!

lolol! Just teasin - thanks - I''ll give it a whirl!

cdj


实际上,我注意到你的例子暗示了另一个级别的xml节点(不是显示的是
)...在这种情况下要么在XPath / XQuery中包含它,要么

替换doc.SelectSingleNode( ...)

doc.DocumentElement.SelectSingleNode(...),它将带你进入

外部元素。


Marc

Actually, I note your example hints at another level of xml nodes (not
shown)... in which case either include this in the XPath/XQuery, or
replace doc.SelectSingleNode(...) with
doc.DocumentElement.SelectSingleNode(...), which will take you inside
the outer element.

Marc


这篇关于将新元素附加到xml文件时出现问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆