为什么我的XDocument在我不想要时保存声明?

时间:2021-08-30 21:17:02

I have the following code:

我有以下代码:

class Program
{
    static void Main(string[] args)
    {
        using (var stream = File.Create(@"C:\test.xml"))
        {
            var xml =
                new XElement("root",
                    new XElement("subelement1", "1"),
                    new XElement("subelement2", "2"));

            var doc = new XDocument(xml);
            doc.Declaration = null;
            doc.Save(stream);
        }
    }
}

I am trying to get XML to save without the xml declaration, but even though I am nulling out the declaration of the XDocument, it is still being saved to the final XML.

我试图在没有xml声明的情况下保存XML,但即使我将XDocument的声明归零,它仍然被保存到最终的XML中。

This code is outputting:

此代码输出:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <subelement1>1</subelement1>
  <subelement2>2</subelement2>
</root>

2 个解决方案

#1


13  

Instead XDocument.Save() you can use XmlWriter with XmlWriterSettings.OmitXmlDeclaration set to true

相反,XDocument.Save()可以使用XmlWriter并将XmlWriterSettings.OmitXmlDeclaration设置为true

using System.IO;
using System.Xml;
using System.Xml.Linq;

XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (var stream = File.Create(@"C:\test.xml"))
using (XmlWriter xw = XmlWriter.Create(stream, xws))
{
    var xml = new XElement(
        "root",
        new XElement("subelement1", "1"),
        new XElement("subelement2", "2"));

    xml.Save(xw);
}

#2


7  

You can do this using XmlWriter with a custom XmlWriterSettings (you'll need a using directive for System.Xml):

您可以使用XmlWriter和自定义XmlWriterSettings来完成此操作(您需要System.Xml的using指令):

using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        var xml =
            new XElement("root",
                         new XElement("subelement1", "1"),
                         new XElement("subelement2", "2"));

        var doc = new XDocument(xml);
        var settings = new XmlWriterSettings
        {
            OmitXmlDeclaration = true
        };
        using (var stream = File.Create(@"test.xml"))
        {
            using (var writer = XmlWriter.Create(stream, settings))
            {
                doc.Save(writer);
            }
        }
    }
}

That's assuming you want to specify the Stream - you can also create an XmlWriter from the filename:

假设您要指定Stream - 您还可以从文件名创建XmlWriter:

using (var writer = XmlWriter.Create("test.xml", settings))
{
    doc.Save(writer);
}

(If you don't need the XDocument for anything else, you can just call Save on the root element, of course, in the same way.)

(如果您不需要其他任何XDocument,您可以以相同的方式在根元素上调用Save。)

#1


13  

Instead XDocument.Save() you can use XmlWriter with XmlWriterSettings.OmitXmlDeclaration set to true

相反,XDocument.Save()可以使用XmlWriter并将XmlWriterSettings.OmitXmlDeclaration设置为true

using System.IO;
using System.Xml;
using System.Xml.Linq;

XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (var stream = File.Create(@"C:\test.xml"))
using (XmlWriter xw = XmlWriter.Create(stream, xws))
{
    var xml = new XElement(
        "root",
        new XElement("subelement1", "1"),
        new XElement("subelement2", "2"));

    xml.Save(xw);
}

#2


7  

You can do this using XmlWriter with a custom XmlWriterSettings (you'll need a using directive for System.Xml):

您可以使用XmlWriter和自定义XmlWriterSettings来完成此操作(您需要System.Xml的using指令):

using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        var xml =
            new XElement("root",
                         new XElement("subelement1", "1"),
                         new XElement("subelement2", "2"));

        var doc = new XDocument(xml);
        var settings = new XmlWriterSettings
        {
            OmitXmlDeclaration = true
        };
        using (var stream = File.Create(@"test.xml"))
        {
            using (var writer = XmlWriter.Create(stream, settings))
            {
                doc.Save(writer);
            }
        }
    }
}

That's assuming you want to specify the Stream - you can also create an XmlWriter from the filename:

假设您要指定Stream - 您还可以从文件名创建XmlWriter:

using (var writer = XmlWriter.Create("test.xml", settings))
{
    doc.Save(writer);
}

(If you don't need the XDocument for anything else, you can just call Save on the root element, of course, in the same way.)

(如果您不需要其他任何XDocument,您可以以相同的方式在根元素上调用Save。)