建立在C#中一个XmlNode / XmlElement的没有一个XmlDocument? [英] Creating an XmlNode/XmlElement in C# without an XmlDocument?

查看:195
本文介绍了建立在C#中一个XmlNode / XmlElement的没有一个XmlDocument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类,基本上只是存放着一些价值观。我已经覆盖了的ToString()方法返回一个不错的字符串表示。

I have a simple class that essentially just holds some values. I have overridden the ToString() method to return a nice string representation.

现在,我想创建一个 toxml用于()方法,将返回这样的事情:

Now, I want to create a ToXml() method, that will return something like this:

<Song>
    <Artist>Bla</Artist>
    <Title>Foo</Title>
</Song>



当然,我可以只使用一个的StringBuilder 在这里,但我想回到一个的XmlNode 的XmlElement XmlDocument.AppendChild 。

Of course, I could just use a StringBuilder here, but I would like to return an XmlNode or XmlElement, to be used with XmlDocument.AppendChild.

我似乎不能够创建一个的XmlElement 比调用其他 XmlDocument.CreateElement ,所以我不知道我刚才忽略任何东西,或者如果我真的要么在任何一个的XmlDocument REF的XmlElement 的工作,或者有函数返回一个包含我想要的XML字符串?

I do not seem to be able to create an XmlElement other than calling XmlDocument.CreateElement, so I wonder if I have just overlooked anything, or if I really either have to pass in either a XmlDocument or ref XmlElement to work with, or have the function return a String that contains the XML I want?

推荐答案

您可能想看看如何使用.NET的内置功能序列化和反序列化对象到XML,而不是创建一个每类本质上只是一个数据传输对象toxml用于()方法。

You may want to look at how you can use the built-in features of .NET to serialize and deserialize an object into XML, rather than creating a ToXML() method on every class that is essentially just a Data Transfer Object.

我已经成功地使用这些技术的几个项目但没有实施细则方便现在。我会尝试用一段时间后,我自己的例子来更新我的答案

I have used these techniques successfully on a couple of projects but don’t have the implementation details handy right now. I will try to update my answer with my own examples sometime later.

下面是几个例子,谷歌返回:

Here's a couple of examples that Google returned:

由Venkat在廉 http://www.agiledeveloper.com/articles/XMLSerialization.pdf

如何序列化和反序列化对象到XML HTTP: //www.dotnetfunda.com/articles/article98.aspx

How to Serialize and Deserialize an object into XML http://www.dotnetfunda.com/articles/article98.aspx

自定义使用.NET XML属性的.NET对象XML序列化的 HTTP:// blogs.microsoft.co.il/blogs/rotemb/archive/2008/07/27/customize-your-net-object-xml-serialization-with-net-xml-attributes.aspx

Customize your .NET object XML serialization with .NET XML attributes http://blogs.microsoft.co.il/blogs/rotemb/archive/2008/07/27/customize-your-net-object-xml-serialization-with-net-xml-attributes.aspx

这篇关于建立在C#中一个XmlNode / XmlElement的没有一个XmlDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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