Gil Sand
Gil Sand

Reputation: 6040

Saving XML file to disk

I'm trying to save an XDocument object to disk, as an .xml file. The object seems well formed in the debugger, I'm only having an issue saving the actual file. Here is what I'm doing :

       //ofx is an `XElement` containing many sub-elements of `XElement` type
        XDocument document = new XDocument(ofx);

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.Indent = true;

        //destinationPath is a string containing a path, like this
        // e.g : "C:\\Users\\Myself\\Desktop"  
        using (var stream = File.Create(destionationPath + @"export.xml"))
        {
            List<byte[]> header = new List<byte[]>();

            byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);

            List<string> headers = new List<string>();
            headers.Add("foo"); 

            // Just adding some headers here on the top of the file. 
            // I'm pretty sure this is irrelevant for my problem

            foreach (string item in headers)
            {
                header.Add(Encoding.ASCII.GetBytes(item));
                header.Add(newline);
            }

            header.Add(newline);

            byte[] bytes = header.SelectMany(a => a).ToArray();

            stream.Write(bytes, 0, bytes.Length);
            using (var writer = XmlWriter.Create(stream, settings))
            {
                document.Save(writer);
            }

        }

Whenever I call this, I have no file in the target directory.

Notes : The directory is well-formed too, as well as the file name. This is verified in the code.

Upvotes: 1

Views: 3107

Answers (1)

Andrzej Gis
Andrzej Gis

Reputation: 14306

Probably you're creating the path badly. Following the comment it you have

string destionationPath = "C:\\Users\\Myself\\Desktop";
destionationPath + @"export.xml" // evaluates to: "C:\\Users\\Myself\\Desktopexport.xml"

You are missing "\\" between directory and file name.

The following code works fine for me.

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Tmp();
        }

        public static void Tmp()
        {

            //ofx is an `XElement` containing many sub-elements of `XElement` type
            XDocument document = new XDocument(new XElement("myName", "myContent"));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;

            //destinationPath is a string containing a path, like this
            // e.g : "C:\\Users\\Myself\\Desktop"  
            string destionationPath = "D:\\Temp\\xml_test\\";
            using (var stream = File.Create(destionationPath + @"export.xml"))
            {
                List<byte[]> header = new List<byte[]>();

                byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);

                List<string> headers = new List<string>();
                headers.Add("foo");

                // Just adding some headers here on the top of the file. 
                // I'm pretty sure this is irrelevant for my problem

                foreach (string item in headers)
                {
                    header.Add(Encoding.ASCII.GetBytes(item));
                    header.Add(newline);
                }

                header.Add(newline);

                byte[] bytes = header.SelectMany(a => a).ToArray();

                stream.Write(bytes, 0, bytes.Length);
                using (var writer = XmlWriter.Create(stream, settings))
                {
                    document.Save(writer);
                }
            }
        }
    }
}

To avoid such situations never create create path by yoursef. Use Path class instead:

Path.Combine(destionationPath, @"export.xml")

Upvotes: 2

Related Questions