英文:
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.
<?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>
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!
答案1
得分: 1
XML中的注释使其无效。但您可能只是为了在您的帖子中进行文档记录而这样做。为了测试,我在Notepad++中创建了这个文件。Notepad++可以告诉您您的XML文件是否包含错误:
这是我使用的代码。主要更改是在末尾添加了保存操作。使用正确的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:
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 = @"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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论