我怎样写在我的XML文件的innerText东西

我能够从这样的文件中读取的标签particualar:

 protected void Page_Load(object sender, EventArgs e)
    {// this is to read from xml.
        if (!Page.IsPostBack)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(@"C:\configfolder\config.xml");

            XmlNodeList portNo = xmlDoc.GetElementsByTagName("AgentConfigRepository");
            foreach (XmlNode node in portNo)
            {
                XmlElement bookElement = (XmlElement)node;
                string no = bookElement.GetElementsByTagName("OVERRIDE_CONFIG_FILE_NAME")[0].InnerText;
                TextBox1.Text = no;
            }
        }
    }

现在我想在OVERRIDE_CONFIG_FILE_NAME将InnerText改变值

这是我的xml文件看起来像:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<AgentConfigRepository>
  <SERVER_SHARE_SW_DIR_NAME val="singleVal">AgentSW</SERVER_SHARE_SW_DIR_NAME>
  <OVERRIDE_CONFIG_FILE_NAME val="singleVal">override_config.xml</OVERRIDE_CONFIG_FILE_NAME>
  <MAINTAIN_AGENT_SW_LEVEL val="singleVal">1.0</MAINTAIN_AGENT_SW_LEVEL>
  <MAINTAIN_AGENT_SW_PATCH_LEVEL val="singleVal">0</MAINTAIN_AGENT_SW_PATCH_LEVEL>
</AgentConfigRepository>

,所以我想override_config.xml改变到在文本框中的某个其它值。

任何建议..感谢

有帮助吗?

解决方案

如果你可以使用的XDocument,它变得非常容易的:

XDocument xdoc = XDocument.Load(@"C:\configfolder\config.xml");
xdoc.Root.Element("OVERRIDE_CONFIG_FILE_NAME").SetValue("HelloThere");
xdoc.Save(@"C:\so2.xml");

其他提示

很不幸,这是目前未经测试(我不是在测试它的位置),但是从你的问题的样子,你正在试图改变元素的innerText你在这一行中:

bookElement.GetElementsByTagName("OVERRIDE_CONFIG_FILE_NAME")[0].InnerText;

要无论是在你的文本框中。通常你想有一个说法是这样的:

bookElement.GetElementsByTagName("OVERRIDE_CONFIG_FILE_NAME")[0].InnerText = "new text"

新的文本可以从在应用程式文本框或其他变量或只是硬编码(如在这个例子中)的字符串。希望这有助于。

可以只设置的innerText像任何其他属性(如添c。所述)

当你做到这一点,但是,它只设置它在XmlDocument对象。为了看到文件中的变化,你必须做保存更改回原来的文件:

bookElement.save(文件名)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top