有没有办法让XDocument在使用ToString方法时打印xml版本?输出这样的东西:
<?xml version="1.0"?>
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...Run Code Online (Sandbox Code Playgroud)
我有以下内容:
var xdoc = new XDocument(new XDocumentType("Response", null, null, "\n"), ...Run Code Online (Sandbox Code Playgroud)
这将打印出这个很好的,但它缺少如上所述的"<?xml版本".
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...Run Code Online (Sandbox Code Playgroud)
我知道你可以通过手动输出我自己来做到这一点.只是想知道是否可以使用XDocument.
以下代码生成此输出:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<customers>
<customer>
<firstName>Jim</firstName>
<lastName>Smith</lastName>
</customer>
</customers>
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它encoding="utf-8"代替encoding="utf-16"?
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace test_xml2
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer> {
new Customer {FirstName="Jim", LastName="Smith", Age=27},
new Customer {FirstName="Hank", LastName="Moore", Age=28},
new Customer {FirstName="Jay", LastName="Smythe", Age=44},
new Customer {FirstName="Angie", LastName="Thompson", Age=25},
new Customer {FirstName="Sarah", LastName="Conners", Age=66}
};
Console.WriteLine(BuildXmlWithLINQ(customers));
Console.ReadLine();
}
private static string BuildXmlWithLINQ(List<Customer> customers)
{
XDocument xdoc =
new …Run Code Online (Sandbox Code Playgroud)