首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 网络编程 → XML专区 → 文章内容

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

作者:郁郁小蝎  来源:中国站长学院  发布时间:2007-2-6 16:47:48
图八中代码演示了把一个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)

  • 相关文章:
  • [图文]循序渐进学习Power Builder 6.0(4-3)
  • 循序渐进学习Power Builder 6.0(14-3)
  • XML在.net平台下的自定义控件的应用(3)
  • XML在.net平台下的自定义控件的应用(2)
  • XML在.net平台下的自定义控件的应用(1)
  • 在.NET Framework中轻松处理XML数据(5-2)
  • 在.NET Framework中轻松处理XML数据(5-1)
  • 在.NET Framework中轻松处理XML数据(4-4)
  • 在.NET Framework中轻松处理XML数据(4-3)
  • [图文]在.NET Framework中轻松处理XML数据(4-2)
  • 在.NET Framework中轻松处理XML数据(4-1)
  • [图文]在.NET Framework中轻松处理XML数据(3-2)
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号