前几天一老鸟跑来说:“小Q,看来这几天你没有什么事情,来给你安排一下工作!”我曰:“哦”,
~~~。从此就开始饿补.net中操作XML的东东。好了,废话不说,做先!
下面是的代码是XML的模板TestTemplate.vxml
1
<?xml version ="1.0" encoding ="utf-8" ?>
2
<vxml xmls="http://www.w3.org/2001/vxml">
3
<form>
4
<var name="val"/>
5
<assign name="val" expr="8"/>
6
<block>
7
<if cond="val < 9">
8
<prompt>此值小于9</prompt>
9
</if>
10
<if cond="val > 2">
11
<prompt>此值大于2</prompt>
12
</if>
13
</block>
14
</form>
15
</vxml>
<?xml version ="1.0" encoding ="utf-8" ?>2
<vxml xmls="http://www.w3.org/2001/vxml">3
<form>4
<var name="val"/>5
<assign name="val" expr="8"/>6
<block>7
<if cond="val < 9">8
<prompt>此值小于9</prompt>9
</if>10
<if cond="val > 2">11
<prompt>此值大于2</prompt>12
</if>13
</block>14
</form>15
</vxml>在这个模板中保存的是VoiceXML文件,这是用来制定语音流程的东东,通过它您就能够实现类似10086或118114的语音台的功能。(VoiceXML小弟正在学习,以后会写些相关的随笔。)
下面的这行代码就是Vxml.aspx中的内容。注意:必须要将除下面这行代码之外的所有内容删除,只有这样才能以流的方式输出来。
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Vxml.aspx.cs" Inherits="Vxml_Vxml" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Vxml.aspx.cs" Inherits="Vxml_Vxml" %>汗颜,就为了前面的这行代码我整整浪费了一个下午,郁闷!
下面的这行代码就是我今天的成果,它的名字就叫“Vxml.aspx.cs”!哈哈~~“呆,骂规骂,别打呀!~~~救命呀!-----咚”。(从桌子下爬了起来,
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.IO;
12
using System.Xml;
13
public 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.SetAttribute("zizi", "……");
50
test.AppendChild(node);
51
node.InnerXml = "……";
52
test.InsertBefore(node, test.LastChild);//将新建的“老鼠”节点给“form”
53
doc.Save(Response.OutputStream);//将修改后的xml文件以流的方式输出到当前页面。
54
}
55
}
56
}
57
}
58![]()
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.IO;12
using System.Xml;13
public partial class Vxml_Vxml : System.Web.UI.Page14
{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.SetAttribute("zizi", "……");50
test.AppendChild(node);51
node.InnerXml = "……";52
test.InsertBefore(node, test.LastChild);//将新建的“老鼠”节点给“form”53
doc.Save(Response.OutputStream);//将修改后的xml文件以流的方式输出到当前页面。54
}55
}56
}57
}58

看看,这就是结果。
1
<?xml version="1.0" encoding="utf-8" ?>
2
<vxml xmls="http://www.w3.org/2001/vxml">
3
<form>
4
<var name="老鼠" />
5
<assign name="老鼠" expr="8" />
6
<block>
7
<if cond="老鼠 < 9">
8
<prompt>此值小于9</prompt>
9
</if>
10
<if cond="老鼠 > 2">
11
<prompt>此值大于2</prompt>
12
</if>
13
</block>
14
<老鼠 zizi="……">……</老鼠>
15
</form>
16
</vxml>
<?xml version="1.0" encoding="utf-8" ?> 2
<vxml xmls="http://www.w3.org/2001/vxml">3
<form>4
<var name="老鼠" /> 5
<assign name="老鼠" expr="8" /> 6
<block>7
<if cond="老鼠 < 9">8
<prompt>此值小于9</prompt> 9
</if>10
<if cond="老鼠 > 2">11
<prompt>此值大于2</prompt> 12
</if>13
</block>14
<老鼠 zizi="……">……</老鼠> 15
</form>16
</vxml>好了,写完了,真累!哦,下班了!走。


浙公网安备 33010602011771号