Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xdocument does not print declaration

I try to use the domainpeople.com API and to do I need to use XML.

Currently I have an error saying "apiProtocol is not found" I guess then that my Xml document is malformed.

The Current xml sent is :

<apiProtocol version="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="checkrequest.xsd">
  <checkRequest user="ifuzion" password="fish4gold121" reference="123456789">
    <domain name="google.com" /> 
  </checkRequest>
</apiProtocol>

Apparently the <?xml?> part does not print out.

My code is basically something similar to :

XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Books"));

(I stripped my code for a question of simplicity but the structure is exactly similar).

Is there any reason why XDocument doesn't print out the <?xml?> part ? It seems that with XmlDocument it works but not with XDocument ... any hints ?

like image 405
Erick Avatar asked Jun 29 '09 19:06

Erick


3 Answers

How are you printing out the contents of your XDocument?

The .ToString() method does not include the xml header, but the .Save() method does.

Edit: The same answer was given here.

like image 126
CoderDennis Avatar answered Oct 18 '22 15:10

CoderDennis


How do you save it? If I do the following, the xml declaration comes out as it should:

XDocument xDocument = new XDocument(
    new XDeclaration("1.0", "UTF-8", "yes"),
    new XElement("Books"));
xDocument.Save(@"c:\temp\file.xml");

The output looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Books />

However, if I instead pass an XmlWriter instance, it seems as if the settings for that XmlWriter overrides what is stated in the XDocument:

XDocument xDocument = new XDocument(
    new XDeclaration("1.0", "UTF-8", "yes"),
    new XElement("Books"));
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
    xDocument.Save(writer);
}
Console.WriteLine(sb.ToString());

The output looks like this:

<?xml version="1.0" encoding="utf-16" standalone="yes"?><Books />

Note how the encoding changed to "utf-16" and the indentation has changed. If you add an XmlWriterSettings instance indicating the encoding (and any other settings you want to control), you get a better result. The following code does what you expect:

XDocument xDocument = new XDocument(
    new XDeclaration("1.0", "UTF-8", "yes"),
    new XElement("Books"));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Indent = true;

using (XmlWriter writer = XmlWriter.Create(@"c:\temp\xdocument.xml", settings))
{
    xDocument.Save(writer);
}

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Books />
like image 32
Fredrik Mörk Avatar answered Oct 18 '22 14:10

Fredrik Mörk


Solution for serialization to string:

// Default encode as Utf8
Encoding outputEncoding = new UTF8Encoding(/*bom*/false);

// Try to use Xml encoding
if (xml.Declaration != null &&
    xml.Declaration.Encoding.ToNonNull().ToLower() != System.Text.Encoding.UTF8.WebName)
{
    outputEncoding = System.Text.Encoding.GetEncoding(xml.Declaration.Encoding);
}

using (var stream = new MemoryStream())
{
    using (new XmlTextWriter(stream, outputEncoding))
    {
        xml.Save(stream);
    }

    return outputEncoding.GetString(stream.ToArray());
}
like image 37
user1257110 Avatar answered Oct 18 '22 15:10

user1257110