使用LINQ创建XML文件

时间:2014-10-14 03:32:23

标签: c# xml linq

我正在使用以下代码生成xml文件。 xml文件正在成功生成。但问题是 - 在文件的开头我得到了一些特殊字符。因此,xml验证失败。

xml文件开头的特殊字符: -

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

XML文件生成代码: -

XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This is a comment"),
new XElement("Persons",
    new XElement("Person", new XAttribute("name", "John Smith"),
        new XElement("Age", new XText("30")),
        new XElement("Gender", new XText("Male"))),
    new XElement("Person", new XAttribute("name", "Mike Folley"),
        new XElement("Age", new XText("25")),
        new XElement("Gender", new XText("Male")))));

doc.Save(@"C:\sample2.xml");

0 个答案:

没有答案