Solved

C# XDocument.Save adds unwanted encoding information

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 64
Create XML 5 53
How to implement an additional command 16 47
CURL sending XML without spaces PHP 5 38
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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