相关疑难解决方法(0)

如何使用XDocument打印<?xml version ="1.0"?>

有没有办法让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.

c# xml linq-to-xml

61
推荐指数
3
解决办法
4万
查看次数

如何强制XDocument在声明行中输出"UTF-8"?

以下代码生成此输出:

<?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)

c# xml utf-8 linq-to-xml

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

linq-to-xml ×2

xml ×2

utf-8 ×1