在 C# 中调整 XmlSerializer 的输出 [英] Tweak output from XmlSerializer in C#

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

问题描述

我想知道是否有一种方法可以让 C# 中的 XmlSerializer 更改它为一个对象的一个​​属性输出的内容.例如,如果我有类似的东西:

I am wondering if there is a way to get XmlSerializer in C# to change what it outputs for one property of one object. For example, if I have something like:

public class MyClass
{
    public string prop1{get;}
    public uint prop2{get;}
    public MyClass2 class2{get;}
    public MyClass3 class3{get;}
}

public class MyClass2
{
    public string prop3{get;}
}

public class MyClass3
{
    //Lots of properties that I want to serialize as normal
}

现在在我的代码中,我有这样的东西:

Now in somewhere in my code, I have something like this:

private void SerializeStuff(List<MyClass> list, string path)
{
    XmlSerializer serialize = new XmlSerializer(typeof(List<MyClass>));
    TextWriter writer = new StreamWriter(path);
    serialize.Serialize(writer, list);
    writer.Close();
}

我想要的是序列化正常工作,但 prop3 替换为其他一些东西.示例:

What I want is for the serialization to work as normal, but with prop3 replaced with some other stuff. Example:

<MyClass>
    <prop1>whatever</prop1>
    <prop2>345</prop2>
    <class2>
        <somethingNotProp3>whatever</somethingNotProp3>
        <somethingElseNotProp3>whatever</somethingElseNotProp3>
    </class2>
    <class3>
        ...
    </class3>
</MyClass>

有没有办法自定义 XmlSerializer 这样我就不必手动编写整个 Xml 文件,或者没有办法做到这一点?

Is there a way to customize XmlSerializer so I don't have to write the entire Xml file manually, or is there no way to do that?

我很确定该解决方案可能与实现 ISerializableGetObjectData 方法有关;但是,我不确定如何实现它.我尝试让 MyClass2ISerializable 继承并实现 GetObjectData,但没有任何改变.prop3 仍然在 XML 文件中输出.

I am pretty sure that the solution could have something to do with implementing ISerializable's GetObjectData method; however, I am not exactly sure how to implement it. I tried making MyClass2 inherit from ISerializable and implementing GetObjectData, but nothing changed. prop3 was still output in the XML file.

推荐答案

使用 System.Xml.Serialization 命名空间 影响实例序列化的方式.

Use the attributes in the System.Xml.Serialization namespace to affect the way that the instance is serialized.

如果您需要属性无法处理的 MyClass3 非常专业的序列化,则实现 IXmlSerializable 接口 这将使您完全控制序列化过程(您甚至可以根据实例决定如何序列化内容/属性).

If you need very specialized serialization for MyClass3 that the attributes cannot handle, then implement the IXmlSerializable interface which will give you complete control over the serialization process (you can even decide on an instance-by-instance basis how to serialize the content/property).

根据评论,您似乎想要实现IXmlSerializable,因为您想要更改属性的名称​​和值;属性将允许您更改属性/元素/属性的名称,但不允许您对值执行转换(我假设您不想破坏您的表示并为此添加额外的属性/值).

Based on the comments, it would seem that you want to implement IXmlSerializable, as you want to change the name of the property and the value; the attributes will let you change the name of the property/element/attribute, but not allow you to perform transformations on the value (and I assume you don't want to corrupt your representation and add an extra property/value for this purpose).

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

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