Search code examples
c#xmllinq-to-xmlxml-encoding

how to give encoding as ISO-8859-1 in xml created using LINQ


I have to create an xml file which has encoding as

<?xml version="1.0" encoding="ISO-8859-1"?>

Currently the xml I am creating using LINQ is having tag as

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

how can I do this using LINQ only.


Solution

  • You should use XDeclaration:

    var d = new XDocument(new XDeclaration("1.0", "ISO-8859-1", ""), new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2")
     ));