XML文件
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tylor</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin redords</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
</CATALOG>
HTML文件
<html>
<head>
<script for="window" event="onload">
xmldso.XMLDocument.load("myfile.xml")
</script>
<script language="javascript">
function movenext()
{
if (xmldso.recordset.absoluteposition <xmldso.recordset.recordcount)
{
xmldso.recordset.movenext()
}
}
function moveprevious()
{
if (xmldso.recordset.absoluteposition >1)
{
xmldso.recordset.moveprevious()
}
}
</script>
<TITLE>CD Navigate</TITLE>
</head>
<body>
<p>
<object WIDTH="0" HEIGHT="0"
CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" ID="xmldso">
</object>
<table>
<tr><td>Title:</td><td><SPAN ID="title" DATASRC=#xmldso DATAFLD="TITLE"></SPAN></td></tr>
<tr><td>Artist:</td><td><SPAN ID="artist" DATASRC=#xmldso DATAFLD="ARTIST"></SPAN></td></tr>
<tr><td>Year:</td><td><SPAN ID="year" DATASRC=#xmldso DATAFLD="YEAR"></SPAN></td></tr>
<tr><td>Country:</td><td><SPAN ID="country" DATASRC=#xmldso DATAFLD="COUNTRY"></SPAN></td></tr>
<tr><td>Company:</td><td><SPAN ID="company" DATASRC=#xmldso DATAFLD="COMPANY"></SPAN></td></tr>
<tr><td>Price:</td><td><SPAN ID="price" DATASRC=#xmldso DATAFLD="PRICE"></SPAN></td></tr>
</table>
<p>
<INPUT TYPE=button VALUE="上一张CD" onCLICK="moveprevious()">
<INPUT TYPE=button VALUE="下一张CD" onCLICK="movenext()">
</p>
</body>
</html>
方法二:
一.下面先建立一个xml文档myxml。myfile为根元素,它包含四个字元素:
<?xml version="1.0" encoding="gb2312"?>
<myfile>
<title>xml实用进阶</title>
<author>魅力奇异岭</author>
<email>zhyt710@tom.com</email>
<date>20060423</date>
</myfile>
二.在建立一个html文档myhtm,用来调用上面建立的xml文档,注意其中javascript的使用。例程如下:
<html>
<head>
<script language="JavaScript" for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("myxml.xml")
nodes = xmlDoc.documentElement.childNodes
title.innerText = nodes.item(0).text
author.innerText = nodes.item(1).text
email.innerText = nodes.item(2).text
date.innerText = nodes.item(3).text
</script>
<title>在HTML中调用XML数据</title>
</head>
<body bgcolor="#FFFFFF">
<b>标题: </b>
<span id="title"></span><br>
<b>作者: </b>
<span id="author"></span><br>
<b>电子信箱: </b>
<span id="email"></span><br>
<b>日期: </b>
<span id="date"></span><br>
</body>
</html>
把两者放在同一个目录下,你就可以打开myhtm.htm来察看myxml.xml的信息了
jscript是不支持写入文件的,ajax的返回值最好是json我这里有个xml转json的函数
客户端
var toObject = function(tagName) {
if (this == null) return null
var seek = false
var retObj = new Object
createObject(retObj, this.get(0))
function createObject(obj, node) {
if (seek) return
try {
if (node.tagName != tagName) {
createObject(obj, node.childNodes[0])
} else {
var childNodes = node.childNodes//初始化节点开始
for (var i = 0i <childNodes.lengthi++) {
var attributes = childNodes[i].attributes
for (var j = 0j <attributes.lengthj++) {
if (attributes[j].name == 'type') {
switch (attributes[j].value) {
case 'int': obj[childNodes[i].tagName] = parseInt(childNodes[i].text || childNodes[i].firstChild.nodeValue)
break
case 'bool': obj[childNodes[i].tagName] = eval(childNodes[i].text || childNodes[i].firstChild.nodeValue)
break
case 'array': //初始化为字符或整数数组
obj[childNodes[i].tagName] = createArray(childNodes[i] || childNodes[i].firstChild.nodeValue)
break
default: obj[childNodes[i].tagName] = childNodes[i].text || childNodes[i].firstChild.nodeValue
}
}
}
}
seek = true
}
} catch (e) { obj = nullseek = truealert('xml初始化json失败!') }
}
function createArray(node) {
var list = new Array()
var childNodes = node.childNodes
for (var i = 0i <childNodes.lengthi++) {
if (childNodes[i].tagName == 'int') {
list[list.length] = parseInt(childNodes[i].text)
} else {
list[list.length] = childNodes[i].text
}
}
return list
}
return retObj
}
服务器端:
/*
* Hashtable t = new Hashtable()
* t.Add("email_bool", "true")
* t.Add("text1_string", "1")
* t.Add("int1_int", 1)
* t.Add("array1_array_string", new string[] { "1", "2" })
* t.Add("array2_array_int", new int[] { 1, 2 })
* Helper.File.CreateAPIXml("user_api", t)
*/
public static void CreateAPIXml(string fileName, Hashtable fileContent){
XmlTextWriter writer = new XmlTextWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8)
writer.WriteStartDocument()
writer.WriteStartElement("user_api")
writer.WriteAttributeString("url", site.Url)
writer.WriteStartElement(fileName)
writer.WriteAttributeString("date", DateTime.Now.ToString("yyyy-MM-dd hh:mm"))
foreach (DictionaryEntry i in fileContent){
string[] key = i.Key.ToString().Split('_')
switch (key[1]){
case "string":
case "int":
case "bool":
writer.WriteStartElement(key[0])
writer.WriteAttributeString("type", key[1])
writer.WriteString(i.Value.ToString())
writer.WriteEndElement()
break
case "array":
writer.WriteStartElement(key[0])
writer.WriteAttributeString("type", key[1])
switch (key[2]){
case "string":
foreach (var v in (string[])i.Value){
writer.WriteElementString(key[2], v.ToString())
}
break
case "int":
foreach (var v in (int[])i.Value){
writer.WriteElementString(key[2], v.ToString())
}
break
}
writer.WriteEndElement()
break
default:
writer.WriteElementString(key[0], i.Value.ToString())
break
}
}
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
writer.Close()
}
返回的xml文件如下:
<user_api>
<emai type="bool">true</email>
<text1 type="string">1</text1>
<int1 type="int">1</int1>
<array1 type="string">
<string>1</string>
<string>2</string>
</array1>
<array2 type="int">
<int>1</int>
<int>2</int>
</array2>
</user_api>
得到的json如下:
{ email: true, string1: '1', int1: 1, array1: ['1','2'], array2: [1,2] }
使用System.Xml命名空间中的XmlDocument类来 *** 作xml的数据C#中对xmlDocument类做了很多优化和封装
可以在内存中创建xml文档也可以读入一个xml文件进行 *** 作
MSDN里有很多示例
绑定后直接调用数据源控件的方法就可以 *** 作了
非常简单
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)