简体   繁体   English

在xml文件中写入innertext

[英]write innertext in xml file

How do i write something in the innertext of my xml file 我如何在我的xml文件的innertext中写一些东西

i am able to read the particualar tag from the file like this: 我能够从文件中读取特定标记,如下所示:

 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;
            }
        }
    }

Now i want to change the value in the innertext of OVERRIDE_CONFIG_FILE_NAME 现在我想更改OVERRIDE_CONFIG_FILE_NAME的innertext中的值

this is how my xml file looks like: 这是我的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>

so i want to change override_config.xml to some other value in the textbox. 所以我想将override_config.xml更改为文本框中的其他值。

any suggestions.. thanks 任何建议..谢谢

If you can use XDocument, it becomes pretty trivial: 如果你可以使用XDocument,它变得非常简单:

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

Unfortunately this is untested at the moment (I am not in a location to test it) but from the looks of your question you are trying to change the innerText of the Element you have found in this line: 不幸的是,目前尚未经过测试(我不在测试它的位置),但从你问题的外观来看,你试图改变你在这一行找到的元素的innerText:

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

To whatever is in your text box. 对于文本框中的任何内容。 Generally you want a statement like this: 通常你想要一个这样的语句:

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

New Text can be the string from a text box in your app or another variable or just hardcoded (as in this example). 新文本可以是应用程序中的文本框或其他变量中的字符串,也可以是硬编码(如本例所示)。 Hope this helps. 希望这可以帮助。

You can just set the InnerText like any other property (As Tim C said) 您可以像任何其他属性一样设置InnerText(正如Tim C所说)

When you do this, however, it only sets it in the XmlDocument object. 但是,当您执行此操作时,它仅在XmlDocument对象中设置它。 In order to see the change in the file, you have to do save the changes back to the file: 要查看文件中的更改,您必须将更改保存回文件:

bookElement.save(filename) bookElement.save(文件名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM