在C#中的XML序列化 [英] xml serialization in c#

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

问题描述

我的类看起来非常类似:

My class looks very similar to this:

[Serializable]
class ExampleClass {
    public bool Value { get; set; }
    public string Names { get; set; }
}

我想它序列化到这一点: 对于值==真:

I would like to serialize it to this: for Value == true :

<ExampleClass>
   <Value>
   <Names>SomeName</Names>
</ExampleClass>

和值==假的:

and for Value == false :

<ExampleClass>
   <Value/>
   <Names>SomeName</Names>
</ExampleClass>

正如你所看到的价值==真的,我们得到一个开始标记和没有属性。在情况下,当值是假的,我们只能得到结束标记。

As you can see for Value==true we get an opening tag and no attributes. In case when Value is false we get only closing tag.

我怎样才能做到这一点用C#序列化?

How can I achieve this with c# serialization?

推荐答案

您似乎想无效的XML。

You seem to want invalid XML.

没有工具或API可以让你做到这一点,你必须使用的TextWriter来写自己的XML。

No tool or API will let you do that, you'll have to write your own XML using a TextWriter.

当然,最好的建议是的没有的做到这一点。重新考虑你的解决方案。

The best advice of course is not to do this. Rethink your solution.

这篇关于在C#中的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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