我有以下代码可以在 xml 文件中写入一些数据。它工作得很好,但属性。我无法为元素创建属性及其值。
//.xml file===========================
<?xml version="1.0" encoding="utf-8"?>
<Errors>
<Error Name="abc" ContactNo="123">
<Description>Test</Description>
</Error>
</Errors>
// c# code ===========================
XmlDocument xmlErrors = new XmlDocument();
xmlErrors.Load(Path.Combine(Application.StartupPath, "Errors.xml"));
XmlElement subRoot = xmlErrors.CreateElement("Error");
// subRoot.Attributes[0].Value = "Test 1";
// subRoot.Attributes[1].Value = "Test 2";
XmlElement Description = xmlErrors.CreateElement("Description");
Description.InnerText = currentData.ExamineeName;
subRoot.AppendChild(Description);
xmlErrors.DocumentElement.AppendChild(subRoot);
xmlErrors.Save(Path.Combine(Application.StartupPath, "Errors.xml"));
你能帮我如何创建属性及其值吗? 谢谢。
最佳答案
XmlElement error = Errors.CreateElement("Error");
XmlAttribute errName= Errors.CreateAttribute("Name");
errName.value="abc"
error.Attributes.Append(errName);
关于c# - 在 C# 中创建 XML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996501/