我想将一个新的 XML 文件写入磁盘,但以下代码会出错。
static void Main(string[] args)
{
using (XmlWriter writer = XmlWriter.Create(@"C:\abc.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
writer.WriteEndElement();
writer.WriteEndDocument();
}
Console.ReadKey();
}
谁能帮我解决这个问题?
注意:abc.xml 尚不存在。
最佳答案
很明显,您无权访问 C:
。选择您有权访问的路径或以更高权限运行应用程序。
作为旁注,对于大多数情况,不建议再使用 System.Xml
,而是使用 LINQ to XML (System.Xml.Linq
):
new XElement("Employees").Save("abc.xml"); // and a path that you have access to.
关于C# XmlWriter.Create() - 访问路径被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554782/