How can I force XDocument to output “UTF-8” in the declaration line?

前端 未结 3 1797
误落风尘
误落风尘 2020-12-20 13:46

The following code produces this output:



  
    

        
3条回答
  •  囚心锁ツ
    2020-12-20 14:44

    Allow me answer my own question, this seems to work:

    private static string BuildXmlWithLINQ()
    {
        XDocument xdoc = new XDocument
        (
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("customers",
                new XElement("customer",
                    new XElement("firstName", "Jim"),
                    new XElement("lastName", "Smith")
                )
            )
        );
        return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
    }
    

提交回复
热议问题