views:

1685

answers:

3

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 ?

+4  A: 

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.

Dennis Palmer
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
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
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
A: 

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

orandov
+2  A: 

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 />
Fredrik Mörk