Updating Value of XML Tag in C#

huangapple go评论57阅读模式
英文:

Updating Value of XML Tag in C#

问题

I am using a XML file as the main configuration (config.xml) source in my C# project, and therefore I need to be able to read data from and write data to this file. Included is the beginning of my XML which contains the tag I would like to update from within the app.

<?xml version="1.0" encoding="utf-8"?>
<root>
	<startupparams>
		<backColor>white</backColor> <!-- Tag to update -->
		<foreColor>black</foreColor>
	</startupparams>
	<strings>
		<appTitle>TheCalc</appTitle>
		<appAuthor>Matthew Croft</appAuthor>
		<appGreeting>Welcome</appGreeting>
	</strings>

The problem is that I am not too familiar with C# and XML integration, and writing (or more accurately updating) the tags to the file is the process that is causing me issue as I am not able to successfully do it. What I tried is included below.

Console.Write("Background Color: ");
string background = Console.ReadLine()!;
Console.WriteLine(background);
Console.WriteLine();
Console.Write("Foreground Color: ");
string foreground = Console.ReadLine()!;
XmlNodeList backColors = config.GetElementsByTagName("backColor");
for (int i = 0; i < backColors.Count; i++)
{
    backColors[i].InnerText = background;
}

Keep in mind that I have already opened the XML by using the following.

XmlDocument config = new XmlDocument();
string configFilePath = @"C:\Users\mcroftdev\Documents\TheCalc\config.xml";
config.Load(configFilePath);

Any suggestions you can provide would be greatly appreciated. Thank you in advance!

英文:

I am using a XML file as the main configuration (config.xml) source in my C# project, and therefore I need to be able to read data from and write data to this file. Included is the beginning of my XML which contains the tag I would like to update from within the app.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;root&gt;
	&lt;startupparams&gt;
		&lt;backColor&gt;white&lt;/backColor&gt;``` &lt; -- Tag to update
		``` &lt;foreColor&gt;black&lt;/foreColor&gt;
	&lt;/startupparams&gt;
	&lt;strings&gt;
		&lt;appTitle&gt;TheCalc&lt;/appTitle&gt;
		&lt;appAuthor&gt;Matthew Croft&lt;/appAuthor&gt;
		&lt;appGreeting&gt;Welcome&lt;/appGreeting&gt;

The problem is that I am not too familiar with C# and XML integration, and writing (or more accurately updating) the tags to the file is the process that is causing me issue as I am not able to successfully do it. What I tried is included below.

Console.Write(&quot;Background Color: &quot;);
string background = Console.ReadLine()!;
Console.WriteLine(background);
Console.WriteLine();
Console.Write(&quot;Foreground Color: &quot;);
string foreground = Console.ReadLine()!;
XmlNodeList backColors = config.GetElementsByTagName(&quot;backColor&quot;);
for (int i = 0; i &lt; backColors.Count; i++)
{
    backColors[i].InnerText = background;
}

Keep in mind that I have already opened the XML by using the following.

XmlDocument config = new XmlDocument();
string configFilePath = @&quot;C:\Users\mcroftdev\Documents\TheCalc\config.xml&quot;;
config.Load(configFilePath);

Any suggestions you can provide would be greatly appreciated. Thank you in advance!

答案1

得分: 1

XML中的注释使其无效。但您可能只是为了在您的帖子中进行文档记录而这样做。为了测试,我在Notepad++中创建了这个文件。Notepad++可以告诉您您的XML文件是否包含错误:

Updating Value of XML Tag in C#

这是我使用的代码。主要更改是在末尾添加了保存操作。使用正确的XML文件并添加保存操作后,代码按预期工作。

XmlDocument config = new XmlDocument();
string configFilePath = @"d:\test.xml";
config.Load(configFilePath);
XmlNodeList backColors = config.GetElementsByTagName("backColor");
for (int i = 0; i < backColors.Count; i++)
{
  backColors[i].InnerText = "new background";
}
config.Save(configFilePath);
英文:

The commenting in your xml made it invalid. But you may have done that just for documentation in your post. For testing I created this file in Notepad++. Notepad++ can tell you if your XML file contains errors:

Updating Value of XML Tag in C#

This is the code I used. The primary change was to add a Save at the end. With a proper XML file and adding the Save the code works as intended.

XmlDocument config = new XmlDocument();
string configFilePath = @&quot;d:\test.xml&quot;;
config.Load(configFilePath);
XmlNodeList backColors = config.GetElementsByTagName(&quot;backColor&quot;);
for (int i = 0; i &lt; backColors.Count; i++)
{
  backColors[i].InnerText = &quot;new background&quot;;
}
config.Save(configFilePath);

huangapple
  • 本文由 发表于 2023年6月13日 10:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定