使用 XmlSerializer 自定义序列化
我有一个类需要执行一些自定义 XML 输出,因此我实现了 IXmlSerialized 接口。但是,除了我想更改 xml 标记名称之外,我希望使用默认序列化输出一些字段。当我调用 serializer.Serialize 时,我会在 XML 中获得默认标记名称。我可以以某种方式改变这些吗?
这是我的代码:
public class myClass: IXmlSerializable
{
//Some fields here that I do the custom serializing on
...
// These fields I want the default serialization on except for tag names
public string[] BatchId { get; set; }
...
... ReadXml and GetSchema methods are here ...
public void WriteXml(XmlWriter writer)
{
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
serializer.Serialize(writer, BatchId);
... same for the other fields ...
// This method does my custom xml stuff
writeCustomXml(writer);
}
// My custom xml method is here and works fine
...
}
这是我的 Xml 输出:
<MyClass>
<ArrayOfString>
<string>2643-15-17</string>
<string>2642-15-17</string>
...
</ArrayOfString>
... My custom Xml that is correct ..
</MyClass>
我想要最终得到的是:
<MyClass>
<BatchId>
<id>2643-15-17</id>
<id>2642-15-17</id>
...
</BatchId>
... My custom Xml that is correct ..
</MyClass>
I have a class that I need to do some custom XML output from, thus I implement the IXmlSerializable interface. However, some of the fields I want to output with the default serialization except I want to change the xml tag names. When I call serializer.Serialize, I get default tag names in the XML. Can I change these somehow?
Here is my code:
public class myClass: IXmlSerializable
{
//Some fields here that I do the custom serializing on
...
// These fields I want the default serialization on except for tag names
public string[] BatchId { get; set; }
...
... ReadXml and GetSchema methods are here ...
public void WriteXml(XmlWriter writer)
{
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
serializer.Serialize(writer, BatchId);
... same for the other fields ...
// This method does my custom xml stuff
writeCustomXml(writer);
}
// My custom xml method is here and works fine
...
}
Here is my Xml output:
<MyClass>
<ArrayOfString>
<string>2643-15-17</string>
<string>2642-15-17</string>
...
</ArrayOfString>
... My custom Xml that is correct ..
</MyClass>
What I want to end up with is:
<MyClass>
<BatchId>
<id>2643-15-17</id>
<id>2642-15-17</id>
...
</BatchId>
... My custom Xml that is correct ..
</MyClass>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在许多情况下,您可以使用接受
XmlAttributeOverrides
的XmlSerializer
构造函数重载来指定此额外名称信息(例如,传递新的XmlRootAttribute) - 但是,据我所知,这不适用于数组。我希望对于
string[]
示例,手动编写会更简单。在大多数情况下,IXmlSerialized
需要大量额外工作 - 由于类似的原因,我会尽可能避免使用它。对不起。In many cases, you can use the
XmlSerializer
constructor-overload that accepts aXmlAttributeOverrides
to specify this extra name information (for example, passing a newXmlRootAttribute
) - however, this doesn't work for arrays AFAIK. I expect for thestring[]
example it would be simpler to just write it manually. In most cases,IXmlSerializable
is a lot of extra work - I avoid it as far as possible for reasons like this. Sorry.您可以使用属性标记字段,以控制序列化的 XML 。例如,添加以下属性:
可能会帮助您实现这一目标。
You can tag your fields with attributes to control the serialized XML. For example, adding the following attributes:
will probably get you there.
如果有人仍在寻找这个,您绝对可以使用 XmlArrayItem,但这需要是类中的属性。
为了便于阅读,您应该使用同一个单词的复数和单数。
大卫
If anyone is still looking for this you can definitely use the XmlArrayItem however this needs to be a property in a class.
For readability you should use the plural and singular of the same word.
David