简体   繁体   English

c#中的xml序列化

[英]xml serialization in c#

My class looks very similar to this:我的 class 看起来与此非常相似:

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

I would like to serialize it to this: for Value == true:我想将其序列化为:对于 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.如您所见,对于 Value==true,我们得到一个开始标签,没有属性。 In case when Value is false we get only closing tag.如果 Value 为 false,我们只会得到结束标签。

How can I achieve this with c# serialization?如何通过 c# 序列化来实现这一点?

Could I ask why you want to deliberately misuse the .NET libraries?请问您为什么要故意滥用 .NET 库? As has been mentioned, what you're asking for is invalid XML, and most classes you'll find in the framework are specifically designed to avoid mistakes like mismatched tags.如前所述,您要求的是无效的 XML,并且您在框架中找到的大多数类都是专门设计用于避免标签不匹配等错误。 So the likely answer is, you can't .所以可能的答案是,你不能

Someone can probably find you a better solution if you describe the problem in more detail.如果您更详细地描述问题,有人可能会为您找到更好的解决方案。

You seem to want invalid XML.您似乎想要无效的 XML。

No tool or API will let you do that, you'll have to write your own XML using a TextWriter.没有工具或 API 会让你这样做,你必须使用 TextWriter 编写自己的 XML。

The best advice of course is not to do this.最好的建议当然是要这样做。 Rethink your solution.重新考虑您的解决方案。

As others have said, what you are asking to produce is not valid XML.正如其他人所说,您要求生产的不是有效的 XML。

I once had to produce an 'xml' file that had a non-printing character in a certain place.我曾经不得不制作一个在某个地方有一个非打印字符的“xml”文件。 Of course, thats not XML, but you still might get asked to do it.当然,这不是 XML,但您仍然可能会被要求这样做。

Lets assume that you can't get hold of the people specifying the protocol and call on the wrath of the XML-gods to strike them with lightning.让我们假设您无法抓住指定协议的人,并呼吁 XML 之神的愤怒用闪电击中他们。

If you need to produce something that is a-bit-like-xml-but-not-quite, running it through a standard XML serializer into a string variable and then doing a search/replace might be your best option.如果您需要生成有点像 xml 但不完全的东西,通过标准 XML 序列化程序将其运行到字符串变量中,然后进行搜索/替换可能是您的最佳选择。 Something like:就像是:

XmlSerializer serializer = new XmlSerializer(someobj.GetType());
string asString = null;
using (StringWriter writer = new StringWriter())
{
    serializer.Serialize(writer, someobj);
    asString = writer.ToString();
}
string weirdXml = asString.Replace("<Value>True</Value>","<Value>").Replace("<Value>False</Value>","<Value/>");

(make sure you pray for forgiveness to the XML-gods each time you compile the code though) (确保每次编译代码时都向 XML 之神祈祷)

I have a feeling that all you really asking for is someway to convert an object to XML and you don't really care about the implementing the ISerializable interface.我有一种感觉,您真正要求的只是以某种方式将 object 转换为 XML 而您并不真正关心实现ISerializable接口。

If that's the case just use TextWriter and write your text file according to your object.如果是这种情况,只需使用TextWriter并根据您的 object 编写文本文件。

Well, as correctly said by everyone, what you are trying to do in case of value = true is invalid attribute.好吧,正如每个人所说的那样,在 value = true 的情况下你试图做的是无效属性。

Instead, as i assume that you want the value thing to be present in the XML only when it is true, add it as an attribute.相反,因为我假设您希望仅在 XML 为真时才将其作为属性添加。 or say as an optional attribute.或者说作为可选属性。 Which is present only when the value is true.仅当值为 true 时才存在。

Like: When true,喜欢:如果是真的,

 <ExampleClass value=true>           
 <Names>SomeName</Names>  
 </ExampleClass>

and when its false当它是假的

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

You will have to add the following attributes to the property in class:您必须将以下属性添加到 class 中的属性:

 [System.Xml.Serialization.XmlAttributeAttribute()]
 [System.ComponentModel.DefaultValueAttribute(false)]

When the value of the element/attribute is equal to default value, it is not included in the generated xml..当元素/属性的值等于默认值时,不包含在生成的 xml..

Hope this helps.希望这可以帮助。

There's a way to do this (even for this crappy protocol you use):有一种方法可以做到这一点(即使对于你使用的这个糟糕的协议):

[XmlIgnore]
public bool Value { get; set; }

[XmlText]
public string ValueString
{
    get
    {
        return Value ? "<Value>" : "<Value/>";
    }
    set
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM