0

I'm trying to create an xml file. I already set the document and have a result with Xmlwriter when printing to console but when it comes to having an actual .xml file on my desktop I always end up with empty files. Clearly I'm missing something or forgetting something but can't tell on my own.

Below is the piece of my code where it all happens (not).

public void button1_Click(object sender, EventArgs e)
    {
        XmlDocument dddxml = new XmlDocument();

        //XmlDeclaration xmldecl;
        //xmldecl = dddxml.CreateXmlDeclaration("1.0", null, null);
        //xmldecl.Encoding = "UTF-8";
        //xmldecl.Standalone = "yes"; 

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;


        StringBuilder builder = new StringBuilder();


        writer = XmlWriter.Create(builder, settings);

        writer.WriteStartDocument();
        writer.WriteStartElement("root");
        BlockSelect(0);

        writer.WriteEndElement();
        writer.WriteEndDocument();

        writer.Close();


        Console.WriteLine(builder.ToString());

        writer = XmlWriter.Create("DddXml.Xml", settings);
        dddxml.Save(writer);
        File.Create(path);//declared elsewhere, valid file location string
    }
3
  • File.Create(path) will create an empty file. If you're trying to write the XML to that file, then you need to put path where "DddXml.Xml" is. Also, XmlWriter is IDisposable - you should use it inside a using statement to ensure it is flushed and closed when finished. Commented Jul 24, 2015 at 7:33
  • have a look in here: dotnetperls.com/xmlwriter Commented Jul 24, 2015 at 7:41
  • changed it as writer = XmlWriter.Create(path, settings); but have no idea about IDisposable stuff. Thanks a lot, will look into it now. Commented Jul 24, 2015 at 7:48

3 Answers 3

2

You have created new XmlDocument here:

XmlDocument dddxml = new XmlDocument();

But you haven't populated it in the rest of the code and in fact you're not using it and writing xml to string builder using WriteStartDocument and WriteEndElement methods of XmlWriter.

Thus your dddxml remains empty, so when you're trying to save it like this:

dddxml.Save(writer);

, there is nothing to save and you're getting empty file.

So you have to choose - will you use XmlDocument or XmlWriter to create and save your xml.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm fairly new to C#, so many unknowns for me still. Things you wrote are exactly what was said to me yesterday by my supervisor. In a less explaining and more vague way :) I'd rather go with Xmlwriter (rest of the code is based on methods using xmlwriter) but have no idea about what to do next.
@EgeBayrak, well if you want to use XmlWriter - then use it. ;) Just create writer pointing not to stringbuilder, but to the real file instead.
2

As commented by @Charles Mager, File.Create() just makes an empty file.

You can try to write directly to the file instead of using StringBuilder. Here's a sample to directly write to the file using the XmlWriter:

 XmlWriter writer = XmlWriter.Create("C:\\ddxml.xml", settings);
 writer.WriteStartDocument();
 writer.WriteStartElement("root");
 writer.WriteEndElement();
 writer.WriteEndDocument();
 writer.Close();

See that the file is written on C:\ddxml.xml.

Comments

1

If you want you can also use LINQ, it's easier :

XDocument doc = new XDocument();
XNamespace ns = "";
doc.Add(new XElement(ns + "root"));
doc.Save(@"C:\DddXml.Xml");

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.