几个 XmlTextReader 的例子, 帮了我大忙.

此博客展示了一段C#代码,定义了一个名为bar的类,其中包含Main方法及三个测试方法Test1、Test2、Test3。每个测试方法都对XML文本进行读写操作,使用了XmlTextReader和XmlTextWriter,分别采用不同方式处理输入的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 = "<?xml version='1.0' encoding='utf-16'?><foo><bar /></foo>";
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 = "<?xml version='1.0' encoding='utf-16'?><foo><bar /></foo>";
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 = "<?xml version='1.0' encoding='utf-16'?><foo><bar /></foo>";
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);
}


}
}

转载于:https://www.cnblogs.com/razor/archive/2004/04/22/7228.html

XmlException: invalid data. System.Xml.XmlStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) Mono.Xml2.XmlTextReader.ReadTextReader (Int32 remained) Mono.Xml2.XmlTextReader.PeekChar () Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () UnityEditor.AndroidXmlDocument..ctor (System.String path) UnityEditor.AndroidManifest..ctor (System.String path) UnityEditor.Android.PostProcessAndroidPlayer.CopyMainManifest (System.String target, System.String playerPackage) UnityEditor.Android.PostProcessAndroidPlayer.PostProcessInternal (System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args) UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:316) UnityEditor.HostView:OnGUI()
最新发布
07-11
XmlException: Attribute name and qualified name must be identical. file:///D:/Lua_Dating538/Assets/Plugins/Android/AndroidManifest.xml Line 3, position 61. Mono.Xml2.XmlTextReader.ReadStartTag () Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) UnityEditor.AndroidXmlDocument..ctor (System.String path) UnityEditor.AndroidManifest..ctor (System.String path) UnityEditor.Android.PostProcessAndroidPlayer.CopyMainManifest (System.String target, System.String playerPackage) UnityEditor.Android.PostProcessAndroidPlayer.PostProcessInternal (System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args) UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:316) UnityEditor.HostView:OnGUI()
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值