叶子树logo
首 页 技术教程 新闻资讯 网站展示 酷站欣赏 下载中心 站长故事 信息互动 论坛交流
Web www.webshu.net
 
  最新推荐→

 
  最新热门→

 
  相关文章→
 您现在的位置: 叶子树 >> 技术教程 >> 网络编程 >> XML教程 >> 正文

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

作者:佚名    文章来源:不详    点击数:    更新时间:2008-4-28           

 

<% if request("infoid")<>"" then set rs=conn.execute("select * from nproduct where id="&request("infoid")) if not (rs.eof and rs.bof) then proname=rs("proname") content=rs("proinfo") end if rs.close set rs=nothing end if %>
图八中代码演示了把一个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();



}

}


叶子树:www.webshu.net
  • 下一篇文章:

  • 文章录入:webshu    责任编辑:webshu 
    叶子树(www.webshu.net)所有资料源于作者发布或网友推荐收集整理而来,仅供学习使用,版权归原作者所有,如有侵权,请您联系我们,我们将尽快更正。

      网友评论:(评论内容只代表网友观点,与本站立场无关!) 发表评论

    友情链接 | 留言互动 | 版权声明
    Copyright©All return the ye ze shu and www.webshu.net   
    本站广告服务请加QQ:904166(超越-激情)
    京ICP备05086028号  把"叶子树" 与你的好友一起分享!