|
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 strFile)
{
// Load the xml file into XmlDocument object.
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(strFile);
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
// Now create StringWriter object to get data from xml document.
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
|