好的,我现在使用文档方法来编写我的 XML 而不是 XmlWriter。我已经用它编写了我的 XML 文件。
userNode = xmlDoc.CreateElement("user");
attribute = xmlDoc.CreateAttribute("age");
attribute.Value = "39";
userNode.Attributes.Append(attribute);
userNode.InnerText = "Jane Doe";
rootNode.AppendChild(userNode);
Run Code Online (Sandbox Code Playgroud)
但问题又是如何读回这些设置。
<users>
<user name="John Doe" age="42" />
<user name="Jane Doe" age="39" />
</users>
Run Code Online (Sandbox Code Playgroud)
文件的格式我可以弄清楚如何读取年龄变量,但无法掌握 name 属性。我的 XML 文件与上面略有不同,但差别不大