Creating an XmlNode/XmlElement in C# without an XmlDocument?
- by Michael Stum
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?