燮preSS XSI:无,但仍显示空元素在.net序列化时, [英] Suppress xsi:nil but still show Empty Element when Serializing in .Net

查看:155
本文介绍了燮preSS XSI:无,但仍显示空元素在.net序列化时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了20+的字符串属性C#类。我着手一个第四那些的一个实际值。我想序列化类,并得到一个输出

I have a c# class that has 20+ string properties. I set about a fourth of those to an actual value. I would like to serialize the class and get an output of

<EmptyAttribute></EmptyAttribute>

有关属性

public string EmptyAttribute {get;set;}

我不希望输出为

I do not want the output to be

<EmptyAttribute xsi:nil="true"></EmptyAttribute>

我使用的是下面的类

I am using the following class

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(string filename) : base(filename,Encoding.UTF8) { }

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

这样我可以得到充分的标签。我只是不知道如何摆脱的XSI:无

so that I can get the full tags. I just don't know how to get rid of the xsi:nil.

推荐答案

我居然能想出解决办法。我知道它有点在某些方面是黑客攻击的,但是这是我得到它的工作

I was actually able to figure this out. I know its a bit of a hack in some ways but this is how I got it to work

 System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(header.GetType());
        XmlTextWriterFull writer = new XmlTextWriterFull(FilePath);
        x.Serialize(writer, header);
        writer.Flush();
        writer.BaseStream.Dispose();
        string xml = File.ReadAllText(FilePath);
        xml = xml.Replace(" xsi:nil=\"true\"", "");
        File.WriteAllText(FilePath, xml);

希望这可以帮助别人了。

Hope this helps someone else out

这篇关于燮preSS XSI:无,但仍显示空元素在.net序列化时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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