我有一个小问题...首先,我必须读取xml文件并在datagridview中提取Arg.2 ...然后我必须更新datagrid中的值。最后,当按下更新按钮时,我需要将此值再次写入Arg.2并使用新名称保存xml文件。
我可以在提取时读取值,并在消息框中更新但我无法在xml文件中看到它...请告诉我有什么问题。
下面是button_click下的代码:
openFileDialog1.ShowDialog();
XmlDocument Newdoc = new XmlDocument();
Newdoc.Load(openFileDialog1.FileName);
w = dataGridView1.Rows[0].Cells["Arg2"].Value.ToString();
Newdoc.SelectSingleNode(".//event[@type='2VO']/properties/media[@Arg2]").InnerText = w;
MessageBox.Show(Newdoc.SelectSingleNode(".//event[@type='2VO']/properties/media[@Arg2]").InnerText);
Newdoc.Save(@"C:\download\updatedxml.xml");
奇怪的是,当检查新的updatedxml文件时,我看到该值在一个非常奇怪的位置得到更新...见下文(2100位置):
<event type="2VO">
<properties>
<schedule startType="-ParentEnd" startOffset="00:00:33:00" endType="Duration" endOffset="00:00:22:00" />
<event title="Pixel VO" reconcileKey="106251137" />
<mediaStream>
<cg type="PIXEL CG" />
<allocation type="ListStream">
<listStream type="Fixed" listStreamNo="0" />
</allocation>
</mediaStream>
<media RuleCode="2VO" Arg1="TUE" Arg2="1940" Arg3="O1T13810" Arg4="" Arg5="" Arg6="" Arg7="" Arg8="">2100</media>
这不是需要的位置......我的Xpath有什么问题吗?
这样做,检查验证像node exist
或不检查你的XPath
try
{
XmlDocument doc = new XmlDocument();
doc.Load(yourDoc); //load youe xml doc
doc.SelectSingleNode("your node").InnerText = "new text";//select your node which you want to update
doc .Save(@"C:\download\updatedxml.xml");
//then show your message box
}
catch(exception ex)
{
//catch exception here
}
然后显示您的消息框
我想出了问题......这是Xpath
的一个问题...所以当我把它修改为(.//@Arg.2).innertext
时,它工作得很好。