16

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 ?

Erick
  • 5,969
  • 10
  • 42
  • 61

4 Answers4

17

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.

Community
  • 1
  • 1
CoderDennis
  • 13,642
  • 9
  • 69
  • 105
  • Yup just saw that I needed a stringwriter .... other than that it seems that I can put anything in the encoding type for the declaration it will still use UTF-16... currently searching to fix that – Erick Jun 29 '09 at 20:13
  • 3
    Did you find a fix for the UTF-16 issue? I experience the same thing. I guess it's because strings in .NET are UTF-16, and if you're not encoding to a byte sequence it will always be UTF-16? – User Sep 24 '09 at 05:30
  • It looks like Mörk's answer offers a potential fix (http://stackoverflow.com/questions/1060164/xdocument-does-not-print-declaration/1060282#1060282), but you have to manually translate between an `XDeclaration.Encoding` string and an actual output `Encoding` format. – patridge Feb 17 '10 at 18:59
10

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 />
Brian Oliver
  • 1,407
  • 2
  • 15
  • 25
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Fine when writing to a file. Note that if you write to a StringBuilder, StringWriter, etc., doing `settings.Encoding = Encoding.UTF8` will not suffice. You will still get `` (with `16`) because the internal representation of strings in .NET is UTF-16. – Jeppe Stig Nielsen Nov 16 '18 at 15:11
1

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());
}
Filip Cornelissen
  • 3,682
  • 3
  • 31
  • 41
0

Did you try saving the XDocument?

xDocument.Save("books.xml");

Look at this question how-to-print-xml-version1-0-using-xdocument

Here is the documentation: XDocument.Save Method

Community
  • 1
  • 1
orandov
  • 3,256
  • 3
  • 32
  • 32