How to convert xml file into a String object

This is a very simple tip on how to load a XML file into XmlDocument object and then load it into a String object. This is kind of a handy utility because when you call ToString method on XmlDocument object, it does not return you its contents but it returns the full class name System.Xml.XmlDocument.

static string GetXmlString(string file)
	// Load the xml file into XmlDocument object.
	XmlDocument xmlDoc = new XmlDocument();
	catch (XmlException e)
	// Now create StringWriter object to get data from xml document.
	StringWriter sw = new StringWriter();
	XmlTextWriter xw = new XmlTextWriter(sw);
	return sw.ToString();
comments powered by Disqus

Blog Tags