Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to read XML file from Server.MapPath and I want to convert it as string.

Please help
Posted

 
Share this answer
 
C#
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();
}	

Hope this will help.
 
Share this answer
 
Try something like below,

Private string GetXmlString(string strFile)
{
//string strFile = HttpContext.Current.Server.MapPath("~/YourFile.xml");	//if you want to use server.mappath use this and change your path accordingly
	XmlDocument xmlDoc = new XmlDocument();	
	xmlDoc.Load(strFile);
	
	// Use StringWriter object to get data from xml-document.
	StringWriter sw = new StringWriter();
	XmlTextWriter xw = new XmlTextWriter(sw);
	xmlDoc.WriteTo(xw);
	return sw.ToString();
}

Hope this helps...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900