C# XmlWriter.Create() - 访问路径被拒绝

标签 c#

我想将一个新的 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/

相关文章:

c# - 如何在C#中获取连续字符?

c# - 共享点 2010 : field formulas

c# - typeof 为未构造的嵌套泛型提供异常 'Type expected`

c# - 更改我的服务器端 WebAPI 以获得异步/等待的好处

c# - 检查Keys是否为字母/数字/特殊符号

c# - IIS 池回收后的 session 引用(并发字典变为空白)

c# - c和代码标签之间的区别

c# - 更改 iOS 设备会使 iOS 应用程序崩溃

c# - 使用内在函数时如何避免 `out`参数错误?

javascript - 如何在asp.net中将下拉列表中的复选框的选定选项显示到文本框