C# XDocument.Save adds unwanted encoding information

Posted on 2016-10-02
Medium Priority
Last Modified: 2016-10-02
I have a set of XML files that I load into XDocuments, adjust some data and save.
The original document has the following as the first line
<?xml version="1.0"?>
once saved this becomes
<?xml version="1.0" encoding="UTF-8"?>

I then SFTP these files to another application that parses them. Unfortunately this now causes a XML -1072896682 error.

I used to do this conversion via a perl script using  XML::LibXML which gave me no issues.

Does anyone know how I can save the XDocument without adding this extra information.
var doc = XDocument.Load(file);
                            var q1 = from c in doc.Elements("meeting")
                                     select c;

                            foreach (XElement date in q1)
                                date.Attribute("meetingdate").Value = meetingDate;

                            var q2 = from c in doc.Elements("meeting").Elements("races").Elements("race")
                                     select c;
                            foreach (XElement date in q2)
                                date.Attribute("racedate").Value = meetingDate;
                            doc.Save(Path.Combine(ver4Directory, "Ver4_" + fileNameDate + "_" + key));

Open in new window

Question by:jetbet
  • 3
  • 2

Author Comment

ID: 41825623
Actual output produced by other application is

03/10 09:24:28.765 *** Exception 7: Exception Error -1072896682 Invalid at the top level of the document.
 in "<?xml version="1.0" encoding="utf-8"?>" at line 1, column 1

This file opens correctly in IE and shows as valid XML in editors
LVL 60

Accepted Solution

Geert Bormans earned 2000 total points
ID: 41825637
At first you need to understand that when you pass
<?xml version="1.0"?>
it means the encoding is the default and making that line into
<?xml version="1.0" encoding="UTF-8"?>
is a no-operation (it means the same)
It is up to the processor to choose the detail in the serialisation, and no XML processing application should break on that

I don't think the xml declaration is the issue by the way, I think the BOM is

The output seems to be written with a BOM (byte order mark) and the PERL library seems to have issue with the BOM
Try to see if this helps
LVL 60

Expert Comment

by:Geert Bormans
ID: 41825638
Most editors do understand how to deal with the BOM, that is why you don't see the issue in IE or in editors

Author Closing Comment

ID: 41825646
Changed the same to the following call (as the link recommended) at it works like a charm.


using (var writer = new XmlTextWriter(Path.Combine(ver4Directory, "Ver4_" + fileNameDate + "_" + key), new UTF8Encoding(false)))
LVL 60

Expert Comment

by:Geert Bormans
ID: 41825648

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question