我需要在c#中使用linq与XML代码保存XML文件

本文关键字:XML linq 代码 文件 保存 | 更新日期: 2023-09-27 18:14:53

我写了一个简单的c#代码来生成一个xml文件,最后将其保存在我当前项目存在的pc目的地中,我正在编写代码。但问题是,当我运行代码时,然后在代码的最后一行生成异常,并显示"System. exe"。附加信息:访问路径"C:'Users'admin'Documents'Visual Studio 2013'Projects'ClassLibrary1'ClassLibrary1"被拒绝。下面是我生成和保存xml文件的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ClassLibrary1
{
    class Class14
    {
        public static void Main()
        {
            XDocument xml = new XDocument(
                                new XDeclaration("1.0", "utf-8", "yes"),
                                new XElement("Students",
                                    new XElement("Student", new XAttribute("ID", 101),
                                        new XElement("Name", "kamal"),
                                        new XElement("Gender", "Male"),
                                        new XElement("Marks", "800")),
                                    new XElement("Student", new XAttribute("ID", 102),
                                        new XElement("Name", "Sapna"),
                                        new XElement("Gender", "Female"),
                                        new XElement("Marks", "900")),
                                    new XElement("Student", new XAttribute("ID", 103),
                                        new XElement("Name", "Raju"),
                                        new XElement("Gender", "Male"),
                                        new XElement("Marks", "870"),
                                    new XElement("Student", new XAttribute("ID", 104),
                                        new XElement("Name", "Sushant"),
                                        new XElement("Gender", "Male"),
                                        new XElement("Marks", "700"))
                                    )));
            xml.Save(@"C:'Users'admin'Documents'Visual Studio 2013'Projects'ClassLibrary1'ClassLibrary1");
        }
    }
}

我需要在c#中使用linq与XML代码保存XML文件

似乎你没有访问路径,试试这个

提供XML文件名

xml.Save(@"C:'sample.xml");

如果您想将文件存储在项目文件夹中,请按照以下代码,在您的项目中添加System.Windows.Forms引用。

string path = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Sample.xml");

我认为如果你想写这个位置,你的应用程序必须在适当的凭据下运行,例如,作为Administrator。然后你可以使用担保权,比如

            var dir = "test_folder";
            var fileName = Path.Combine(dir, "test.xml");
            System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
            string user = wi.Name;
            MessageBox.Show(user);
            DirectorySecurity dsec = Directory.GetAccessControl(dir);
            dsec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Modify, AccessControlType.Allow));
            try
            {
                Directory.SetAccessControl(dir, dsec);
                File.WriteAllText(fileName, "test");
            }
            catch (UnauthorizedAccessException uae)
            {
                MessageBox.Show(uae.Message);
            }
            catch (SecurityException se)
            {
                MessageBox.Show(se.Message);
            }