用c#生成xml字串及解析xml字串
生成xml字串:
using (StringWriter sw = new StringWriter())
{
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.WriteStartDocument();
xtw.WriteStartElement("root");
//test
xtw.WriteStartElement("test");
xtw.WriteString("test content");
xtw.WriteEndElement();
xtw.WriteEndElement();//root
xtw.WriteEndDocument();
result = sw.ToString();
}
解析xml:
using System;
using System.IO;
using System.Text;
using System.Xml;
namespace foo {
public class bar {
public static void Main() {
Test1();
Test2();
Test3();
}
public static void Test1() {
string strInput = "
XmlTextReader r = new XmlTextReader(new StringReader(strInput));
StringBuilder sb = new StringBuilder();
XmlTextWriter w = new XmlTextWriter(new StringWriter(sb));
w.WriteNode(r, false);
w.Flush();
string strOutput = sb.ToString();
Console.WriteLine("Input = {0}, Output = {1}", strInput.Length, strOutput.Length);
}
public static void Test2() {
string strInput = "
XmlTextReader r = new XmlTextReader(new StringReader(strInput));
MemoryStream ms = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(ms, Encoding.Unicode);
w.WriteNode(r, false);
w.Flush();
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string strOutput = sr.ReadToEnd();
Console.WriteLine("Input = {0}, Output = {1}", strInput.Length, strOutput.Length);
}
public static void Test3() {
string strInput = "
XmlTextReader r = new XmlTextReader(new StringReader(strInput));
MemoryStream ms = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(ms, Encoding.UTF8);
w.WriteNode(r, false);
w.Flush();
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string strOutput = sr.ReadToEnd();
Console.WriteLine("Input = {0}, Output = {1}", strInput.Length, strOutput.Length);
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/5859/viewspace-915389/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 應用SQLServer For XML 生成XML避免在C# 拼字串SQLServerXMLC#字串
- 用dom4j SAXReader解析xml檔案及字串XML字串
- xml字串轉JSON字串XML字串JSON
- SQL——用FOR XML Path完成字串的聚合SQLXML字串
- C# XML解析C#XML
- C#解析XML檔案C#XML
- XML Schema 字串資料型別及約束詳解XML字串資料型別
- 關於XML字串和XML Document之間的轉換薦XML字串
- .net api介面接收字串或者xmlAPI字串XML
- Python解析XML檔案生成HTMLPythonXMLHTML
- Xml解析XML
- 用Python解析XML(1)PythonXML
- 用Python解析XML(2)PythonXML
- 用Python解析XML(3)PythonXML
- java使用jaxb解析XML(含根據xml自動生成實體類)JavaXML
- JAVA生成XMLJavaXML
- c#(解析xml檔案基礎方法)C#XML
- Python解析xml[xml.dom]PythonXML
- iOS – XML解析iOSXML
- go 解析xmlGoXML
- xml php 解析XMLPHP
- iOS - XML解析iOSXML
- 【Java】解析xmlJavaXML
- Java解析XMLJavaXML
- jaxb解析xmlXML
- DOM解析XMLXML
- 用C#讀寫XML的方法C#XML
- ArkTS 中的 XML 解析與生成:應用開發實戰XML
- C#解析JSON字串總結C#JSON字串
- 讀取xml檔案 解析雙層xmlXML
- js將字串轉換為xml物件程式碼例項JS字串XML物件
- 將多個路徑字串轉換成XML文件樹字串XML
- 序列化篇 生成xml 以及讀取xmlXML
- 175.XML解析XML
- 使用jdom解析XMLXML
- 解析xml筆記XML筆記
- XML解析技術XML
- xml解析之domXML