Xdocument does not print declaration

2020-08-09 09:56发布

问题:

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 ?

回答1:

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.



回答2:

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 />


回答3:

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());
}


回答4:

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