如何更新XML文档 [英] How to update XML doc

查看:89
本文介绍了如何更新XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用xml。



成功保存我的xml文档我更新这个不起作用。

我的代码是



Hi all,

I am working on xml.

successfully save my xml document when I update this not working.
my code is

if (!File.Exists(path))
            {
                //save
                XElement xml = new XElement("Formulas",
                                new XElement("Formula", txtFormula.Text));
                xml.Save(path);
            }
            else
            {
                //update
                XDocument doc = XDocument.Load(path);
                var xmlupdate = doc.Descendants().Elements("Formulas").Where(f => f.Element("Formula").Value == txtFormula.Text).FirstOrDefault();
                doc.Save(path);


            }





我错了。



where I went wrong.

推荐答案





你在条件声明中进行了更新。



试试下面的代码

Hi,

You performed update in conditional statement.

Try below code
if (!File.Exists(path))
            {
                //save
                XElement xml = new XElement("Formulas",
                                new XElement("Formula", txtFormula.Text));
                xml.Save(path);
            }
            else
            {
                //update
                XDocument doc = XDocument.Load(path);
                var xmlupdate = doc.Descendants("Formulas")
                    .FirstOrDefault().Element("Formula").Value = txtFormula.Text;
                doc.Save(path);
            }


对不起,我不会说C,但我认为您可能在使用XDocument时遇到问题。在使用XDocument进行练习之前,使用XElement要容易得多。首先尝试[这个]。请参阅使用XElement而不使用XDocument一节。
Sorry I don''t speak C but I think you may be having an issue with using the XDocument. It is far easier to work with XElement until you get some practice with XDocument. Try [this] for starters. See the section on using XElement without using XDocument.


这篇关于如何更新XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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