读取XML文件并转换为字符串 [英] Read XML file and convert to string

查看:664
本文介绍了读取XML文件并转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想从Server.MapPath读取XML文件,我想将其转换为字符串。



请帮助

Hi,

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

Please help

推荐答案

查看这些



如何使用Visual C#从文件中读取XML# [ ^ ]



将XML文件读入XmlDocument [ ^ ]



问候..
See these

How to read XML from a file by using Visual C#[^]

Read XML file into XmlDocument[^]

Regards..


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.


尝试类似下面的内容,



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...


这篇关于读取XML文件并转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆