快速业务通道

在ASP.net中的XML操作笔记

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-21

前几天一老鸟跑来说:“小Q,看来这几天你没有什么事情,来给你安排一下工作!”我曰:“哦”,~~~。从此就开始饿补.net中操作XML的东东。好了,废话不说,做先!

下面是的代码是XML的模板TestTemplate.vxml

1
2
3
4
5
6
7
8 此值小于9
9
10
11 此值大于2
12
13
14
15

在这个模板中保存的是VoiceXML文件,这是用来制定语音流程的东东,通过它您就能够实现类似10086或118114的语音台的功能。(VoiceXML小弟正在学习,以后会写些相关的随笔。)

下面的这行代码就是Vxml.aspx中的内容。注意:必须要将除下面这行代码之外的所有内容删除,只有这样才能以流的方式输出来。

1

汗颜,就为了前面的这行代码我整整浪费了一个下午,郁闷!

下面的这行代码就是我今天的成果,它的名字就叫“Vxml.aspx.cs”!哈哈~~“呆,骂规骂,别打呀!~~~救命呀!-----咚”。(从桌子下爬了起来,)。在这里必须要应用两个库,就是System.IO;和System.Xml。具体的执行过程是:先读取TestTemplate.vxml;然后,替换模板中相关节点属性的值“val”为“老鼠”;再后,在form节点的最后部分加入<老鼠 zizi="……">……;最后,在本页输出结果。

1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Xml;
13public partial class Vxml_Vxml : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Response.Clear();
18 string path = Server.MapPath("TestTemplate.vxml");
19 //DOM方式读取
20 XmlDocument doc = new XmlDocument();//建立XML文件对象
21 doc.Load(path);//读取xml文件
22 XmlElement objElement = doc.DocumentElement;//获取XML文件的根元素
23 foreach (XmlNode test in objElement.ChildNodes)
24 {
25 if (test.Name == "form")//找到名称为form的节点
26 {
27 foreach (XmlNode test1 in test.ChildNodes)//在form节点中便利节点
28 {//找到在“form”子节点中属性“name”的值为"val"的节点。
29 if (test1.Attributes.Count > 0 && test1.Attributes["name"].InnerXml == "val")
30 {
31 test1.Attributes["name"].InnerXml = "老鼠";//设置name属性的值为“老鼠”
32 }
33 else if (test1.Name == "block")//找到“block”节点
34 {
35 foreach (XmlNode test2 in test1.ChildNodes)
36 {//将“block”节点的所以子节点属性“cond”中的“Val”替换成“老鼠”。
37 if (test2.Attributes["cond"].InnerXml.Contains("val"))
38 {
39 string str = test2.Attributes["cond"].InnerXml;
40 str = "老鼠" + str.Substring(3);//不能用“Replace”真是郁闷
41 test2.Attributes["cond"].InnerXml = str;
42 }
43 }
44 }
45 }
46 XmlElement node = doc.CreateElement("老鼠");//建立“老鼠”节点
47 XmlAttribute attribute = doc.CreateAttribute("zizi");
48 node.SetAttributeNode(attribute);
49 node.SetAttrib

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号