如何将XmlDocumentstring转化为datestring函数

C#如何将XmlDocument转化为string函数 - C#教程 - 编程入门网
C#如何将XmlDocument转化为string函数
/// &summary&
/// 将XmlDocument转化为string
/// &/summary&
/// &param name=&xmlDoc&&&/param&
/// &returns&&/returns&
public string ConvertXmlToString(XmlDocument xmlDoc)
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = Formatting.I
xmlDoc.Save(writer);
StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
stream.Position = 0;
string xmlString = sr.ReadToEnd();
sr.Close();
stream.Close();
return xmlS
本文URL地址:/Programming/csharp/85.htmXmlDocument To String
已有 4254 次阅读
|个人分类:|系统分类:|关键词:XML
寻思怎么将XmlDocument内容 转换成String xml。网上有很多介绍从String xml到XmlDocument的,比较简单:
string xml = &&XML&&Test&Hello World&/Test&&/XML&&XmlDocument doc = new XmlDocument();xml.loadXml(xml);
&&&&&&& 如何将XmlDocument内容 转换成String xml,怎么也找不到,只好写了个特别笨的,先写到本地文件,再字符串形式流读取:
public string XmltoString(string path){&&&&&&&&&&& string strXML = &&;&&&&&&&&&&& string strLine = &&;&&&&&&&&&&& StreamReader objReader = new StreamReader(path);&&&&&&&&&&& // read line&&&&&&&&&&& while ((strLine = objReader.ReadLine()) != null)&&&&&&&&&&& {&&&&&&&&&&&&&&& strXML += strL&&&&&&&&&&& }&&&&&&&&&&& objReader.Close();
&&&&&&&&&&& return strXML;}
后来,在网上看到一篇&&:
// xmlDocument to stringstatic public string xmlDocument2String(XmlDataDocument doc){&&&&&&&&&&& MemoryStream stream = new MemoryStream();&&&&&&&&&&& XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);&&&&&&&&&&& writer.Formatting = Formatting.I&&&&&&&&&&& doc.Save(writer);
&&&&&&&&&&& StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);&&&&&&&&&&& stream.Position = 0;&&&&&&&&&&& string xmlstring = sr.ReadToEnd();&&&&&&&&&&& sr.Close();&&&&&&&&&&& stream.Close();
&&&&&&&&&&&}
挺好!学习...
======== 补充 =========
xmlDocument2String 可以把 xmlDocument 内容变成比较符合标准格式的 xml 字符串,例如:
&?xml version=&1.0& encoding=&utf-8&?&&XML&& &Test&Hello World&/Test&&/XML&
刚刚发现,其实如果只要求字符串的话,不用这么麻烦,xmlDocument 的 OuterXml 方法就可以实现了。结果如下:
&XML&&Test&Hello World&/Test&&/XML&
转载本文请联系原作者获取授权,同时请注明本文来自李芳军科学网博客。链接地址:
上一篇:下一篇:
当前推荐数:1
评论 ( 个评论)
扫一扫,分享此博文
作者的精选博文
作者的其他最新博文
热门博文导读
Powered by
Copyright &Jdom方式进行的XML文件、Document、String之间的相互转换
Jdom方式进行的XML文件、Document、String之间的相互转换
Xml代码 &&version="1.0"&encoding="UTF-8"哈里波特10这是一本很好看的书。&&
Java代码:
Java代码 &import&java.io.*;&&&&import&org.jdom.D&&import&org.jdom.input.SAXB&&import&org.jdom.output.F&&import&org.jdom.output.XMLO&&&&&&&&&&&public&class&OperationXMLByJdom&{&&&&&&&&&&&&&&&&&&public&static&void&doc2XML(Document&doc,&String&filePath)&throws&Exception{&&&&&&&&&&Format&format&=&Format.getCompactFormat();&&&&&&&&&&&format.setEncoding("UTF-8");&&&&&&&&&&&format.setIndent("&&&&&");&&&&&&&&&&&&&&&&XMLOutputter&outputter&=&new&XMLOutputter(format);&&&&&&&&&&FileWriter&writer&=&new&FileWriter(filePath);&&&&&&&&&&outputter.output(doc,&writer);&&&&&&&&&&writer.close();&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&public&static&Document&string2Doc(String&xmlStr)&throws&Exception&{&&&&&&&&&&java.io.Reader&in&=&new&StringReader(xmlStr);&&&&&&&&&&Document&doc&=&(new&SAXBuilder()).build(in);&&&&&&&&&&&&&&&&&return&&&&&&&}&&&&&&&&&&&&&&&&&&&&public&static&String&doc2String(Document&doc)&throws&Exception&{&&&&&&&&&&Format&format&=&Format.getPrettyFormat();&&&&&&&&&&format.setEncoding("UTF-8");&&&&&&&&&&XMLOutputter&xmlout&=&new&XMLOutputter(format);&&&&&&&&&&ByteArrayOutputStream&bo&=&new&ByteArrayOutputStream();&&&&&&&&&&xmlout.output(doc,&bo);&&&&&&&&&&return&bo.toString();&&&&&&}&&&&&&&&&&&&&&&&&&&&public&static&Document&xml2Doc(String&xmlFilePath)&throws&Exception&{&&&&&&&&&&File&file&=&new&File(xmlFilePath);&&&&&&&&&&return&(new&SAXBuilder()).build(file);&&&&&&}&&&&&&&&&&&&public&static&void&main(String[]&args)&{&&&&&&&&&&try{&&&&&&&&&&&&&&Document&doc&=&xml2Doc("test.xml");&&&&&&&&&&&&&&System.out.println(doc);&&&&&&&&&&&&&&System.out.println(doc2String(doc));&&&&&&&&&&&&&&doc&=&string2Doc(doc2String(doc));&&&&&&&&&&&&&&doc2XML(doc,&"1.xml");&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&&&&&&&}&&}&&
输出XML文件:
Java代码 &&?xml&version="1.0"&encoding="UTF-8"?&&&&books&&&&&&&&&book&&&&&&&&&&&&&&name&哈里波特&/name&&&&&&&&&&&&&&price&10&/price&&&&&&&&&&&&&&memo&这是一本很好看的书。&/memo&&&&&&&&&/book&&&&/books&&&
发表评论:
TA的最新馆藏<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&用Java怎么把String类型的字符串转化为XML格式输出?_百度知道

我要回帖

更多关于 string 转化 utf 8 的文章

 

随机推荐