Hello everyone,
I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly.
Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid.
Any better approaches recommended?
thanks in advance,
George
George GeorgePosted May 9, 2008, 3:53 AM
Thanks Mahesh,
What are the pros and cons compared with XMLDocument and XMLTextWriter from functinoal and performance perspective? :-)
regards,
George
Mahesh ChandPosted May 8, 2008, 11:23 AM
There are multiple ways to do it but concatenating strings using StringBuilder is definitely not a good way to do it. I would suggest to use XmlReader, XmlWriter, XmlDocument and DataSet classes, whichever works better for you. XmlDocument is probabaly the best way. You can use its methods and properties to build the entire XML messsage in memory.
Here is an article on how to create XML documents using Xml related classed available in .NET Framework:
http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx
Use XmlDocument methods to add children. Here are members of XmlDocument:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument_members.aspx