• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

C# XDocument.Save adds unwanted encoding information

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

0
jetbet
Asked:
jetbet
  • 3
  • 2
1 Solution
 
jetbetAuthor Commented:
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
0
 
Geert BormansCommented:
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
http://stackoverflow.com/questions/4942825/xdocument-saving-xml-to-file-without-bom
0
 
Geert BormansCommented:
Most editors do understand how to deal with the BOM, that is why you don't see the issue in IE or in editors
0
 
jetbetAuthor Commented:
Changed the same to the following call (as the link recommended) at it works like a charm.

Thanks,

using (var writer = new XmlTextWriter(Path.Combine(ver4Directory, "Ver4_" + fileNameDate + "_" + key), new UTF8Encoding(false)))
   {
     doc.Save(writer);
       }
0
 
Geert BormansCommented:
cheers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now