覆盖xml文件值 [英] Overwrite a xml file value

查看:83
本文介绍了覆盖xml文件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的xml文件

I have a xml file like this

<count>0</count>

现在我希望覆盖值0.如何在c#中做到这一点?

Now I wish to overwrite the value 0. How do I do that in c#?

编辑

<counter>
  <count>0</count>
  <email>
  </email>
</counter>`

这是我的XML文件,我希望在email元素中写入一个值,并且还要更改count元素的值

This is my XML file I wish to write a value in the email element and also change the value of count element

            XmlDocument doc = new XmlDocument();
            doc.Load(COUNTER);
            foreach (XmlNode node in doc.SelectNodes("count"))
            {
                node.InnerText = (count-1).ToString();
            }
            foreach (XmlNode node in doc.SelectNodes("email"))
            {
                node.InnerText = (count - 1).ToString();
            }
            doc.Save(COUNTER); `

执行此操作时,不会将任何值写入文件

When I do this no values are written to the file

推荐答案

您的直接问题是使用doc.SelectNodes("count")而不是doc.GetElementsByTagName("count")

Your direct problem is the use of doc.SelectNodes("count") instead of doc.GetElementsByTagName("count")

这篇关于覆盖xml文件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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