Creating an XmlNode/XmlElement in C# without an XmlDocument?

Posted by Michael Stum on Stack Overflow See other posts from Stack Overflow or by Michael Stum
Published on 2008-10-18T20:07:15Z Indexed on 2010/05/15 22:30 UTC
Read the original article Hit count: 190

Filed under:
|

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

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

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

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.

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml