如何将C#类转换为XMLElement或XMLDocument [英] How do I convert a C# class to an XMLElement or XMLDocument

查看:48
本文介绍了如何将C#类转换为XMLElement或XMLDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C#类,我想使用XMLSerializer进行序列化.但我想将其序列化为XMLElement或XMLDocument.这是可能的还是我必须将其序列化为一个字符串,然后将其解析回XMLDocument?

I have an C# class that I would like to serialize using XMLSerializer. But I would like to have it serialized to a XMLElement or XMLDocument. Is this possible or do I have to serialize it to a String and then parse the string back to a XMLDocument?

推荐答案

您可以创建一个新的XmlDocument,然后调用CreateNavigator().AppendChild().这将为您提供XmlWriter,您可以将其传递给Serialize方法,该方法将转储到文档根目录中.

You can create a new XmlDocument, then call CreateNavigator().AppendChild(). This will give you an XmlWriter you can pass to the Serialize method that will dump into the doc root.

这篇关于如何将C#类转换为XMLElement或XMLDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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