Skip to main content
 首页 » 编程设计

c#之在 System.Console 上查看 XML 声明时出现问题

2024年02月24日18sxdcgaq8080

我一直在试验 LINQ to XML,遇到了一个非常基本的问题。出于某种原因,将树转储到 System.Console 时我没有看到 XML 声明。

using System; 
using System.Xml.Linq; 
 
... 
 
public static void Main(string[] args) 
{ 
    // Build tree. 
    XDocument xd = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); 
 
    // Output tree. 
    System.Console.WriteLine(xd); 
 
    // Pause. 
    System.Console.ReadLine(); 
} 

有人可以解释我做错了什么基本事情吗?

谢谢,

斯科特

请您参考如下方法:

向 XDoc 添加一些真实数据。并确保使用 Save() 方法查看全部内容:

  XDocument xd = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); 
  xd.Add(new XElement("top")); 
  xd.Save(Console.Out);