Solved

C# XDocument.Save adds unwanted encoding information

Posted on 2016-10-02
5
32 Views
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

0
Comment
Question by:jetbet
  • 3
  • 2
5 Comments
 

Author Comment

by:jetbet
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
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 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
http://stackoverflow.com/questions/4942825/xdocument-saving-xml-to-file-without-bom
0
 
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
0
 

Author Closing Comment

by:jetbet
ID: 41825646
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41825648
cheers
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now