Updating Value of XML Tag in C#

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

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.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <root>
  3. <startupparams>
  4. <backColor>white</backColor> <!-- Tag to update -->
  5. <foreColor>black</foreColor>
  6. </startupparams>
  7. <strings>
  8. <appTitle>TheCalc</appTitle>
  9. <appAuthor>Matthew Croft</appAuthor>
  10. <appGreeting>Welcome</appGreeting>
  11. </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.

  1. Console.Write("Background Color: ");
  2. string background = Console.ReadLine()!;
  3. Console.WriteLine(background);
  4. Console.WriteLine();
  5. Console.Write("Foreground Color: ");
  6. string foreground = Console.ReadLine()!;
  7. XmlNodeList backColors = config.GetElementsByTagName("backColor");
  8. for (int i = 0; i < backColors.Count; i++)
  9. {
  10. backColors[i].InnerText = background;
  11. }

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

  1. XmlDocument config = new XmlDocument();
  2. string configFilePath = @"C:\Users\mcroftdev\Documents\TheCalc\config.xml";
  3. 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.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;root&gt;
  3. &lt;startupparams&gt;
  4. &lt;backColor&gt;white&lt;/backColor&gt;``` &lt; -- Tag to update
  5. ``` &lt;foreColor&gt;black&lt;/foreColor&gt;
  6. &lt;/startupparams&gt;
  7. &lt;strings&gt;
  8. &lt;appTitle&gt;TheCalc&lt;/appTitle&gt;
  9. &lt;appAuthor&gt;Matthew Croft&lt;/appAuthor&gt;
  10. &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.

  1. Console.Write(&quot;Background Color: &quot;);
  2. string background = Console.ReadLine()!;
  3. Console.WriteLine(background);
  4. Console.WriteLine();
  5. Console.Write(&quot;Foreground Color: &quot;);
  6. string foreground = Console.ReadLine()!;
  7. XmlNodeList backColors = config.GetElementsByTagName(&quot;backColor&quot;);
  8. for (int i = 0; i &lt; backColors.Count; i++)
  9. {
  10. backColors[i].InnerText = background;
  11. }

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

  1. XmlDocument config = new XmlDocument();
  2. string configFilePath = @&quot;C:\Users\mcroftdev\Documents\TheCalc\config.xml&quot;;
  3. 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文件并添加保存操作后,代码按预期工作。

  1. XmlDocument config = new XmlDocument();
  2. string configFilePath = @"d:\test.xml";
  3. config.Load(configFilePath);
  4. XmlNodeList backColors = config.GetElementsByTagName("backColor");
  5. for (int i = 0; i < backColors.Count; i++)
  6. {
  7. backColors[i].InnerText = "new background";
  8. }
  9. 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.

  1. XmlDocument config = new XmlDocument();
  2. string configFilePath = @&quot;d:\test.xml&quot;;
  3. config.Load(configFilePath);
  4. XmlNodeList backColors = config.GetElementsByTagName(&quot;backColor&quot;);
  5. for (int i = 0; i &lt; backColors.Count; i++)
  6. {
  7. backColors[i].InnerText = &quot;new background&quot;;
  8. }
  9. 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:

确定