在 xml 文件中写入内部文本
我如何在 xml 文件的内部文本中写入一些内容
我能够从文件中读取特定标记,如下所示:
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 的内部文本中的值,
这就是我的 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 更改为文本框中的其他值。
任何建议..谢谢
How do i write something in the innertext of my xml file
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
this is how my xml file looks like:
<?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.
any suggestions.. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以使用 XDocument,那么它就变得非常简单:
If you can use XDocument, it becomes pretty trivial:
不幸的是,目前尚未测试(我无法测试它),但从您的问题的外观来看,您正在尝试更改您在这一行中找到的元素的内部文本:
更改为文本框中的任何内容。通常,您需要这样的语句:
新文本可以是应用程序中文本框中的字符串或另一个变量,或者只是硬编码(如本例所示)。希望这有帮助。
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:
To whatever is in your text box. Generally you want a statement like this:
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.
您可以像任何其他属性一样设置 InnerText(正如 Tim C 所说),
但是,当您这样做时,它只会在 XmlDocument 对象中设置它。为了查看文件中的更改,您必须将更改保存回文件:
bookElement.save(filename)
You can just set the InnerText like any other property (As Tim C said)
When you do this, however, it only sets it in the XmlDocument object. In order to see the change in the file, you have to do save the changes back to the file:
bookElement.save(filename)