Solved

C# XDocument.Save adds unwanted encoding information

Posted on 2016-10-02
5
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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