2

I want to write a new XML file to disk but the following code gives an error.

static void Main(string[] args)
        {
            using (XmlWriter writer = XmlWriter.Create(@"C:\abc.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Employees");
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }            

            Console.ReadKey();       
       }

Can anybody help me with this?

Note: abc.xml does not already exist.

1
  • are you sure error is access to path denied? because in my PC I received "A required privilege is not held by the client." error. Commented Sep 23, 2012 at 18:47

5 Answers 5

3

Obviously, you don't have the right to access C:. Choose a path you have access to or run the application with higher privileges.

As a side note, for most scenarios, it's not recommended to use System.Xml anymore, use LINQ to XML (System.Xml.Linq) instead:

new XElement("Employees").Save("abc.xml"); // and a path that you have access to.
Sign up to request clarification or add additional context in comments.

Comments

2

Depending on the system, you need administrator rights to create files @C:\

Run VS instante as administrator or change code to

 using (XmlWriter writer = XmlWriter.Create("abc.xml"))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("Employees");
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }   

1 Comment

Just to explain the above code, the difference is that in this case the "abc.xml" file is created in the same directory as YourProgram.exe (bin/Debug or bin/Release), which is writable by user running VS
2

Don't wanna necropost, but I just saw this post and I got the same error.

Turns out that the folder was set to:

  • ReadOnly

This disallows C# to write to any of the files in your solution.

To fix this, go to:

  • Right-Click on the main folder that houses all of your C# .sln files
  • Go to Properties
  • At the very bottom of the window will be a check named: Read-only (Only applies to files in folder)

Here is a link to what the above folder looks like: Properties

Really stupid, but something easy to forget!

Comments

1

You can not write file into C:\ from VS without administrator mode. You need to run your application/VS in Admin mode to write file in C:\. Or you can create one folder in C:\ and write file in that folder.

CODE

 using (XmlWriter writer = XmlWriter.Create(@"C:\folder\abc.xml"))
 {
           writer.WriteStartDocument();
           writer.WriteStartElement("Employees");
           writer.WriteEndElement();
           writer.WriteEndDocument();
  }

In above code you don't required to run application/VS in admin mode.

Note: C:\folder must exist otherwise it throws error.

If C:\folder not exist add below code before writing file.

if (System.IO.Directory.Exists(@"C:\folder") == false)
{
    System.IO.Directory.CreateDirectory(@"C:\folder");
}

7 Comments

That's not true: the above code doesn't work without admin privileges.
Your code only works if folder already exists, which it doesn't... I did try before commenting.
You can check with below step: just open one notepad file and try to save it in C:\ you get error message. Now create one folder in C:\ and try to save notepad file. you can save it successfully because you can't save directly file in C:\ but you can save file after create folder (current login user has permission(write& read) on folder) in C:\ .
yes it's obvious folder must be there. I am not saying to save file without folder. I am just gave solution to save file C:\ without running VS/Application in admin mode.
@codesparkle Think a downvote is a little harsh here. I just gave alternate solution. I already mentioned in my answer to create folder in C:\ and after that save file.
|
0

For people who came here with exception

XmlWriter.Create() - Access to path denied

And you have the privilege to the given path you have mentioned, then have to check your file name as well. I had the issue when I was trying to create a file with no name or with no file extension. For these cases as well we will get the above mentioned exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.