用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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- xml字串轉JSON字串XML字串JSON
- C# XML解析C#XML
- XML Schema 字串資料型別及約束詳解XML字串資料型別
- .net api介面接收字串或者xmlAPI字串XML
- Python解析XML檔案生成HTMLPythonXMLHTML
- Xml解析XML
- c#(解析xml檔案基礎方法)C#XML
- java使用jaxb解析XML(含根據xml自動生成實體類)JavaXML
- ArkTS 中的 XML 解析與生成:應用開發實戰XML
- Java解析XMLJavaXML
- go 解析xmlGoXML
- iOS – XML解析iOSXML
- C#解析JSON字串總結C#JSON字串
- 序列化篇 生成xml 以及讀取xmlXML
- C# 操作xml(轉)C#XML
- C# 之 Linq to XmlC#XML
- python之XML解析PythonXML
- 175.XML解析XML
- **xml檔案 columnNumber: 6; 註釋中不允許出現字串 "--"。XML字串
- Python XML解析之DOMPythonXML
- python XML 檔案解析PythonXML
- jdom解析xml檔案XML
- C# StartsWith 字串的例項方法解析C#字串
- C#讀取Xml檔案C#XML
- C# 將HTML轉為XMLC#HTMLXML
- C# 將Excel轉為XMLC#ExcelXML
- C# 解析16進位制字串。將16進位制字串轉換成明文字串C#字串
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS
- XML解析-最快的方式SAXXML
- 使用DocumentBuilderFactory解析XML淺談UIXML
- XML DOM 解析器概述XML
- 使用 Java 解析XML檔案JavaXML
- XML 檔案解析實踐 (DOM 解析)XML
- 【C#】比較 Random 與 RandomNumberGenerator 生成隨機字串C#random隨機字串
- C# 字串操作C#字串
- C# 字串(String)C#字串
- 使用C#讀寫xml檔案C#XML
- C# XML基礎入門(XML檔案內容增刪改查清)C#XML
- C#字尾表示式解析計算字串公式C#字串公式