动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Xml → 阅读文章

 在.NET Framework中轻松处理XML数据(4-3)

作者:郁郁小蝎 来源:中国站长学院 
阅读 2184 人次 , 2006-2-15 14:32:00 

图八中代码演示了把一个string数据转换为Base64 编码的XML流。图九是输出的结果。


Figure 8 Persisting a String Array as Base64

using System;

using System.Text;

using System.IO;

using System.Xml;



class MyBase64Array

{

public static void Main(String[] args)

{

string outputFileName = "test64.xml";

if (args.Length > 0)

outputFileName = args[0]; // file name



// 把数组转换成XML

String[] theArray = {"Rome", "New York", "Sydney", "Stockholm",

"Paris"};



CreateOutput(theArray, outputFileName);

return;

}



private static void CreateOutput(string[] theArray, string filename)

{

// 打开XML writer

XmlTextWriter xmlw = new XmlTextWriter(filename, null);

//使子元素根据 Indentation 和 IndentChar 设置缩进。此选项只对元素内容进行缩进

xmlw.Formatting = Formatting.Indented;

//书写版本为“1.0”的 XML 声明

xmlw.WriteStartDocument();

//写出包含指定文本的注释

xmlw.WriteComment("Array to Base64 XML");

//开始写出array节点

xmlw.WriteStartElement("array");

//写出具有指定的前缀、本地名称、命名空间 URI 和值的属性

xmlw.WriteAttributeString("xmlns", "x", null, "dinoe:msdn-mag");

// 循环的写入array的子节点

foreach(string s in theArray)

{

//写出指定的开始标记并将其与给定的命名空间和前缀关联起来

xmlw.WriteStartElement("x", "element", null);

//把S转换成byte[]数组, 并把byte[]数组编码为 Base64 并写出结果文本,要写入的字节数为s总长度的2倍,一个string占的字节数是2字节。

xmlw.WriteBase64(Encoding.Unicode.GetBytes(s), 0, s.Length*2);

//关闭子节点

xmlw.WriteEndElement();

}

//关闭根节点,只有两级

xmlw.WriteEndDocument();



// 关闭writer

xmlw.Close();



// 读出写入的内容

XmlTextReader reader = new XmlTextReader(filname);

while(reader.Read())

{

//获取节点名为element的节点

if (reader.LocalName == "element")

{



byte[] bytes = new byte[1000];

int n = reader.ReadBase64(bytes, 0, 1000);

string buf = Encoding.Unicode.GetString(bytes);



Console.WriteLine(buf.Substring(0,n));

}

}

reader.Close();



}

}

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:在.NET Framework中轻松处理XML数据(4-2)
· 下一篇:在.NET Framework中轻松处理XML数据(4-4)
· 一个简单的XML Schema的例子
· XML轻松学习手册(5)XML语法之三
· XML介绍系列(4)
· 建立MSXML 测试环境
· XSL简明教程(2)XSL转换


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077