10

我正在使用不导入缩写的空 xml 元素的旧应用程序。例如:

坏空:

<foo />

好空:

<foo></foo>

我知道实现这一点的解决方案,我现在将介绍:

public class XmlTextWriterFull : XmlTextWriter
{


    public XmlTextWriterFull(Stream stream, Encoding enc) : base(stream, enc)
    {
    }

    public XmlTextWriterFull(String str, Encoding enc) : base(str, enc) 
    {
    }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

和客户端代码:

                    var x_settings = new XmlWriterSettings();
                    x_settings.NewLineChars = Environment.NewLine;
                    x_settings.NewLineOnAttributes = true;
                    x_settings.NewLineHandling = NewLineHandling.Replace;
                    x_settings.CloseOutput = true;
                    x_settings.Indent = true;
                    x_settings.NewLineOnAttributes = true;

                    //var memOut = new MemoryStream();
                    var writer = new XmlTextWriterFull(outputFilename, Encoding.UTF8); //Or the encoding of your choice
                    var x_serial = new XmlSerializer(typeof(YOUR_OBJECT_TYPE));
                    x_serial.Serialize(writer, YOUR_OBJECT_INSTANCE);

                    writer.Close();

但是,如果您仔细观察XmlWriterSettings,客户端代码中从未使用过。因此,xml 输出的格式非常糟糕。我的问题是:我如何调整上面的代码来接受XmlWriterSettings

使用工厂创建方法和密封/内部/抽象类使其难以实现覆盖。

我会接受另一种解决方案,我不同意我的上述解决方案。

  • 解决方法

第 1 步:在您的解决方案中创建以下类:

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink) : base(sink)
    {
        Formatting = Formatting.Indented;
    }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

第 2 步:添加以下客户端代码。确保将 YOUR_OBJECT_TYPE 和 YOUR_OBJECT_INSTANCE 替换为您正在使用的类和实例:

TextWriter streamWriter = new StreamWriter(outputFilename);
var writer = new XmlTextWriterFull(streamWriter);

var x_serial = new XmlSerializer(typeof (YOUR_OBJECT_TYPE));
x_serial.Serialize(writer, YOUR_OBJECT_INSTANCE);

writer.Close();

上面的解决方法将产生以下空 xml 元素格式:

<foo>
</foo>

此解决方法的问题在于它添加了换行符(请注意元素位于不同的行上)。这对您来说可能是可以接受的,但会导致我的旧应用程序出现问题。

4

5 回答 5

5

这个怎么样。

XmlWrappingWriterhttp://www.tkachenko.com/blog/archives/000585.html获取很棒的课程(为简洁起见,我省略了代码)。

有了它,我们可以创建一个子类,如下所示(与您原来的非常相似):

public class XmlTextWriterFull2 : XmlWrappingWriter
{
    public XmlTextWriterFull2(XmlWriter baseWriter)
        : base(baseWriter)
    {
    }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

然后可以像这样调用它(再次非常相似):

var x_settings = new XmlWriterSettings();
x_settings.NewLineChars = Environment.NewLine;
x_settings.NewLineOnAttributes = true;
x_settings.NewLineHandling = NewLineHandling.None;
x_settings.CloseOutput = true;
x_settings.Indent = true;
x_settings.NewLineOnAttributes = true;

using (XmlWriter writer = XmlWriter.Create(outputFilename, x_settings))
{
    using (XmlTextWriterFull2 xmlTextWriterFull = new XmlTextWriterFull2(writer))
    {
        var x_serial = new XmlSerializer(typeof(YOUR_OBJECT_TYPE));
        x_serial.Serialize(xmlTextWriterFull, YOUR_OBJECT_INSTANCE);
    }
}

在我的例子中,一个元素之前被渲染为

<Foo>
</Foo>

变成了

<Foo></Foo>

正如您在问题中提到的那样,这实际上是一个非常棘手的问题,因为一切都被密封/内部等,使得覆盖相当困难。我认为我最大的问题是试图让一个XmlWriter接受XmlWriterSettings:除了这种方法,我找不到让原件XmlTextWriterFull尊重给定的方法XmlWriterSettings

MSDN指出这种方法:

XmlWriter.Create(XmlWriter, XmlWriterSettings)

可用于应用XmlWriterSettingsXmlWriter. 我无法让它像我想要的那样工作(例如,缩进从来没有工作过),并且在反编译代码时,似乎所有设置都与这个特定方法一起使用,因此为什么我的调用代码只是传入(outputFile某种流也可以工作)。

于 2012-11-20T10:18:54.913 回答
2

您在问题中提供的解决方案添加了额外的换行符(启用缩进时),因为我们告诉作者将此元素视为有子元素。

这是我如何修改您的解决方法以动态操作缩进以避免那些额外的换行符。

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink)
        : base(sink)
    {
        Formatting = Formatting.Indented;
    }

    private bool inElement = false;

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        base.WriteStartElement(prefix, localName, ns);

        // Remember that we're in the process of defining an element.
        // As soon as a child element is closed, this flag won't be true anymore and we'll know to avoid messing with the indenting.
        this.inElement = true;
    }

    public override void WriteEndElement()
    {
        if (!this.inElement)
        {
            // The element being closed has child elements, so we should just let the writer use it's default behavior.
            base.WriteEndElement();
        }
        else
        {
            // It looks like the element doesn't have children, and we want to avoid emitting a self-closing tag.
            // First, let's temporarily disable any indenting, then force the full closing element tag.
            var prevFormat = this.Formatting;
            this.Formatting = Formatting.None;
            base.WriteFullEndElement();
            this.Formatting = prevFormat;
            this.inElement = false;
        }
    }
}
于 2014-12-05T02:58:36.163 回答
1

以下代码片段强制在同一行上打印结束标记(对不起 vb 版本,使用 C# 重写应该很容易):

Imports System.Xml
Imports System.IO
Public Class CustomXmlTextWriter
    Inherits XmlTextWriter

    Public Sub New(ByRef baseWriter As TextWriter)
        MyBase.New(baseWriter)
        Formatting = Xml.Formatting.Indented
    End Sub

    Public Overrides Sub WriteEndElement()
        If Not (Me.WriteState = Xml.WriteState.Element) Then
            MyBase.WriteEndElement()
        Else
            Formatting = Xml.Formatting.None
            MyBase.WriteFullEndElement()
            Formatting = Xml.Formatting.Indented
        End If
    End Sub

End Class
于 2016-04-19T17:19:22.797 回答
0

另外的选择。

public class XmlCustomTextWriter : XmlTextWriter
{
    private TextWriter _tw = null;

    public XmlCustomTextWriter(TextWriter sink)
        : base(sink)
    {
        _tw = sink;
        Formatting = Formatting.Indented;
        Indentation = 0;
    }

    public void OutputElement(string name, string value)
    {
        WriteStartElement(name);
        string nl = _tw.NewLine;
        _tw.NewLine = "";
        WriteString(value);
        WriteFullEndElement();
        _tw.NewLine = nl;
    }
}
于 2013-02-15T12:58:43.627 回答
0

把它留在这里以防有人需要它;因为上面的答案都没有为我解决,或者看起来有点矫枉过正。

    FileStream fs = new FileStream("file.xml", FileMode.Create);
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter w = XmlWriter.Create(fs, settings);
    w.WriteStartDocument();
    w.WriteStartElement("tag1");

        w.WriteStartElement("tag2");
        w.WriteAttributeString("attr1", "val1");
        w.WriteAttributeString("attr2", "val2");
        w.WriteFullEndElement();

    w.WriteEndElement();
    w.WriteEndDocument();
    w.Flush();
    fs.Close();

诀窍是设置XmlWriterSettings.Indent = true并将其添加到XmlWriter

编辑:

或者,您也可以使用

w.Formatting = Formatting.Indented;

而不是添加 XmlWriterSettings。

于 2015-09-09T08:00:12.817 回答