XmlTextWriter

  • Thread starter Thread starter quest
  • Start date Start date
Q

quest

Is there anyway I can generate the xml in the following format using
XmlTextWriter ?

Intended output:
<?xml version="1.0" ?>

I tried:

XmlTextWriter xmlWriter = new XmlTextWriter(xmlFileName, null);
xmlWriter.Namespaces = false;

but i got :

<?xml version="1.0" standalone="yes" ?>

Thanks.
 
quest said:
Is there anyway I can generate the xml in the following format using
XmlTextWriter ?

Intended output:
<?xml version="1.0" ?>

I tried:

XmlTextWriter xmlWriter = new XmlTextWriter(xmlFileName, null);
xmlWriter.Namespaces = false;

but i got :

<?xml version="1.0" standalone="yes" ?>

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's a sample which doesn't write standalone:

using System;
using System.Xml;

class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.AppendChild (doc.CreateElement("test"));
XmlTextWriter xmlWriter = new XmlTextWriter("test.xml", null);
xmlWriter.Namespaces = false;
doc.Save(xmlWriter);
}
}
 
This is the code (using XmlTextWriter class):

XmlTextWriter xmlWriter = new XmlTextWriter(myfile, null);
xmlWriter.Namespaces = false;
xmlWriter.WriteStartDocument(false);
xmlWriter.WriteEndDocument();

produces : <?xml version="1.0" standalone="no" ?>

Thanks.
 
quest said:
This is the code (using XmlTextWriter class):

XmlTextWriter xmlWriter = new XmlTextWriter(myfile, null);
xmlWriter.Namespaces = false;
xmlWriter.WriteStartDocument(false);

If you simply do
xmlWriter.WriteStartDocument();
without any argument, then you don't get the standalone indication in
the XML declaration.

But note that calling
xmlWriter.WriteEndDocument();

directly after WriteStartDocument will throw an error that the generated
XML is not well-formed as it lacks a root element.
 
Back
Top