简体   繁体   English

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

[英]Why is my XDocument saving the declaration when I don't want it to?

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>

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);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的XDocument保存的文件不完整? - Why is my XDocument saving an incomplete file? 保存xdocument时访问被拒绝 - access denied when saving xdocument 当我单击ListView的ImageButton时为什么不触发回发事件? - Why don't I get my postback event fired when I click on the ImageButton of my ListView? 为什么当我在 DataGrid 中做了一些更改时,我的 ObservableCollection 没有任何更改? - Why I don't have any changes in my ObservableCollection when I have made some in my DataGrid? 我不希望我的datagridview自动生成colums - I don't want my datagridview to generate colums automatically 我的屏幕保护程序正在停止屏幕暂停,但我不希望它 - My screensaver is stopping screen suspend but I don't want it to 出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么 - For some reason my code is spawning in my zombies when it is not supposed to and I don't know why 为什么 XDocument.Parse() 不能正确解析我的 XML? - Why doesn't XDocument.Parse() parse my XML properly? Linq to SQL Join允许Nulls当我不想要那个时 - Linq to SQL Join Allowing Nulls When I don't want that 输入名称时,我的代码将返回StackOverFlowException ...我不明白为什么 - My code returns a StackOverFlowException when I enter a name… I don't understand why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM