XML序列化 - 渲染空元素 [英] Xml Serialization - Render Empty Element

查看:120
本文介绍了XML序列化 - 渲染空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XmlSerializer,并在一个类中的下列财产

I am using the XmlSerializer and have the following property in a class

public string Data { get; set; }



,我需要输出完全像这样

which I need to be output exactly like so

<Data />



我将如何去实现呢?

How would I go about achieving this?

推荐答案

解决这个是创建序列化使用,以确定是否序列化属性或不是 PropertyNameSpecified 属性。例如:

The solution to this was to create a PropertyNameSpecified property that the serializer uses to determine whether to serialize the property or not. For example:

public string Data { get; set; }

[XmlIgnore]
public bool DataSpecified 
{ 
   get { return !String.IsNullOrEmpty(Data); }
   set { return; } //The serializer requires a setter
}

这篇关于XML序列化 - 渲染空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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