需要覆盖XMLWriter的方法 [英] Need to overwrite XMLWriter's method

查看:103
本文介绍了需要覆盖XMLWriter的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要覆盖XMLWriter的方法"WriteElementString",以便在值为空,下面的代码不起作用的情况下不写入元素,尝试了 override new 关键字,但仍然使用框架方法.

I need to overwrite the XMLWriter's method "WriteElementString" to not write the element if the value is empty, the code bellow didn't work, tried override and new keywords but it still goes to the framework method.

public static void WriteElementString(this XmlWriter writer,
                                      string localName,
                                      string value)
{
    if (!string.IsNullOrWhiteSpace(value))
    {
        writer.WriteStartElement(localName);
        writer.WriteString(value);
        writer.WriteEndElement();
    }
}

答案很接近,但正确的解决方案是:

The answer was close but correct solution is:

public abstract class MyWriter : XmlWriter
{
    private readonly XmlWriter writer;
    public Boolean skipEmptyValues;

    public MyWriter(XmlWriter writer)
    {
        if (writer == null) throw new ArgumentNullException("Writer");
        this.writer = writer;
    }

    public new void WriteElementString(string localName, string value)
    {
        if (string.IsNullOrWhiteSpace(value) && skipEmptyValues)
        {
            return;
        }
        else
        {
            writer.WriteElementString(localName, value);
        }
    }
}

推荐答案

您需要创建一个装饰XmlWriter的对象以实现您要执行的操作. 有关装饰者模式的更多信息

You need to create an object that decorates XmlWriter to achieve what you are trying to do. More on the Decorator Pattern

public class MyXmlWriter : XmlWriter
{
    private readonly XmlWriter writer;

    public MyXmlWriter(XmlWriter writer)
    {
        if (writer == null) throw new ArgumentNullException("writer");
        this.writer = writer;
    }

    // This will not be a polymorphic call
    public new void WriteElementString(string localName, string value)
    {
        if (string.IsNullOrWhiteSpace(value)) return;

        this.writer.WriteElementString(localName, value);
    }

    // the rest of the XmlWriter methods will need to be implemented using Decorator Pattern
    // i.e.
    public override void Close()
    {
        this.writer.Close();
    }
    ...
}

在LinqPad中使用以上对象

Using the above object in LinqPad

var xmlBuilder = new StringBuilder();
var xmlSettings = new XmlWriterSettings
{
    Indent = true
};

using (var writer = XmlWriter.Create(xmlBuilder, xmlSettings))
using (var myWriter = new MyXmlWriter(writer))
{
    // must use myWriter here in order for the desired implementation to be called
    // if you pass myWriter to another method must pass it as MyXmlWriter
    //    in order for the desired implementation to be called
    myWriter.WriteStartElement("Root"); 
    myWriter.WriteElementString("Included", "Hey I made it");
    myWriter.WriteElementString("NotIncluded", "");
}

xmlBuilder.ToString().Dump();

输出:

<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Included>Hey I made it</Included>
</Root>

您要尝试做的是使用扩展方法重写方法,这不是他们打算执行的.请参阅扩展方法MSDN上的绑定扩展方法部分页面编译器将始终将WriteElementString解析为XmlWriter实现的实例.您需要手动调用扩展方法XmlWriterExtensions.WriteElementString(writer, localName, value);才能使代码按需执行.

What you are trying to do, is to override a method using an extension method which is not what they are intended to do. See the Binding Extension Methods at Compile Time section on the Extension Methods MSDN Page The compiler will always resolve WriteElementString to the instance implemented by XmlWriter. You would need to manually call your extension method XmlWriterExtensions.WriteElementString(writer, localName, value); in order for your code to execute as you have it.

这篇关于需要覆盖XMLWriter的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆