如何用新值更新XML节点? [英] How to update XML nodes with new values?

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

问题描述

我的 App_Data 文件夹中有一个xml.我需要编辑该xml节点中的值.我尝试过的是-

I have a xml inside my App_Data folder. I need to edit the values in nodes of that xml. What I had tried is-

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("~/App_Data/conf.xml.config"));

        XmlNodeList aNodes = xDoc.SelectNodes("/ConfigInf");
        foreach (XmlNode node in aNodes)
        {
            XmlNode child1 = node.SelectSingleNode("Node1");
            XmlNode child2 = node.SelectSingleNode("Node2");              

            child1.InnerText = "Value1";
            child2.InnerText = "Value2";
        }

我需要用新值重新编写xml,因为当我尝试再次访问同一xml时,它应该包含新值.但是当我访问xml时,仅当我这样调用时,我仍然会获得旧的(初始)值- Test.LoadConf(Server.MapPath("./App_Data/conf.xml.config")); .如何使用新值或其他替代方法(例如,使用新值创建新xml的方式)写入XML?(因为我只需要在单个页面中访问此xml)

I need to re-write the xml with new values as when ever I try to access the same xml again, it should contain the new values. But when I access the xml, I still get the old(initial) values only when I call like this -Test.LoadConf(Server.MapPath("./App_Data/conf.xml.config"));. How to write to XML with new values or any alternative method like create a new xml with new values?(as I need to access this xml in a single page only)

推荐答案

在编辑后调用保存,如果不需要覆盖原始名称,则可以使用不同的名称

call save after edit, you can give diferent name if you don't need to overwrite the original

例如名为 new.conf.xml.config

xDoc.Save(Server.MapPath("~/App_Data/new.conf.xml.config"));

下次您可以照常加载原图

next time you can load the original as usual

xDoc.Load(Server.MapPath("~/App_Data/conf.xml.config"));

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

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