hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

problem with LINQ to XML

Last post 07-17-2008, 7:34 by dabd. 2 replies.
Sort Posts: Previous Next
  •  07-16-2008, 3:35 6398

    problem with LINQ to XML

    Hi,

    When I load an XML file and save it without modification the resulting file contains an invalid character as the first character in the file (just before the header).  It is visible with the Emacs editor as a dot, and the resulting file fails validation with XMLPad for example.

    Is this an encoding problem?

    Here is the code and the example file:

    #light
    let test () =
        let doc = XDocument.Load(@"c:/in.xslt")
        doc.Save("c:/out.xslt")
       
    do test ()


    file in.xslt:

    <?xml version="1.0" encoding="UTF-8"?>
    <a>test</a>


  •  07-16-2008, 8:28 6399 in reply to 6398

    Re: problem with LINQ to XML

    It's most likely a Byte Order Mark, which the .NET libraries generate by default. The only way I've suppressed generation of it is to save the document with an XmlTextWriter initialised with a null Encoding ("If encoding is null it writes out the stream as UTF-8 and omits the encoding attribute from the ProcessingInstruction."), or even with an ecoding of  new UTF8Encoding(false). Check the docs and try it out, ISTR having some problem with settings that were supposed to suppress the BOM not actually doing so.

    Iain.

  •  07-17-2008, 7:34 6406 in reply to 6399

    Re: problem with LINQ to XML

    It was a BOM indeed. Problem solved.

    Thx
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems