xml:
<?xml version="1.0" encoding="utf-8" ?>
<library>
<name>首都图书馆</name>
<address>朝阳区华威桥南</address>
<books>
<book type="math">
<id>0000</id>
</book>
<book type="computer">
<id>0001</id>
<name>Xml初学</name>
<publisher>人民出版社</publisher>
<publishdate>2010-05-0-18</publishdate>
<fee>100.54</fee>
</book>
<book type="computer">
<id>0002</id>
<name>XSD定义</name>
<author>子弟</author>
<publisher>子弟出版社</publisher>
<publishdate>2010-05-0-18</publishdate>
<fee>102.54</fee>
</book>
</books>
</library>
cs:
XmlDocument doc=new XmlDocument();
doc.Load(System.AppDomain.CurrentDomain.BaseDirectory+"/htmlxml/books.xml");
XmlNodeList nodelist = doc.GetElementsByTagName("book");
XmlElement element = doc.GetElementById("b3");
XmlNode node = doc.SelectSingleNode("library/books/book[2]");
XmlNodeList nodelistselect = doc.SelectNodes("library/books/book");
Response.Write("Name: " + node.Name + "<br/>");
Response.Write("NodeType: " + node.NodeType + "<br/>");
Response.Write("Value:" + node.Value + "<br/>");
Response.Write("InnerText: " + node.InnerText + "<br/>");
Response.Write(@"InnerXml: " + node.InnerXml+"<br/>");
Response.Write(@"OuterXml: " + node.OuterXml + "<br/>");
Response.Write("HasChildNodes: " + node.HasChildNodes + "<br/>");
Response.Write("---------------------------------" + "<br/>");
Response.Write("FirstChild: " + node.FirstChild.Name + "<br/>");
Response.Write("LastChild: " + node.LastChild.Name + "<br/>");
Response.Write("---------------------------------" + "<br/>");
Response.Write("Atrribute: <br/>");
foreach (XmlAttribute atrribute in node.Attributes)
{
Response.Write("Name: " + atrribute.Name);
}
Response.Write("----------------------------------" + "<br/>");
Response.Write("node: <br/>");
foreach (XmlNode cnode in node.ChildNodes)
{
if(cnode.NodeType==XmlNodeType.Element&&cnode.FirstChild.NodeType==XmlNodeType.Text)
Response.Write("Name: " + cnode.Name+"<br/>");
}
结果:
Name: book
NodeType: Element
Value:
InnerText: 0001Xml初学人民出版社2010-05-0-18100.54
InnerXml: 0001Xml初学人民出版社2010-05-0-18100.54
OuterXml: 0001Xml初学人民出版社2010-05-0-18100.54
HasChildNodes: True
---------------------------------
FirstChild: id
LastChild: fee
---------------------------------
Atrribute:
Name: type----------------------------------
node:
Name: id
Name: name
Name: publisher
Name: publishdate
Name: fee
js:
function loadXMLDoc(dname) {
var xmlDoc;
// code for IE
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
}
else {
alert('Your browser cannot handle this script');
}
xmlDoc.async = false;
xmlDoc.load(dname);
return (xmlDoc);
}
xmlDoc = loadXMLDoc("/htmlxml/books.xml");
var y = xmlDoc.getElementsByTagName("library/books/book[2]");
var x = y[0];
for (var i = 0; i < x.childNodes.length; i++) {
document.write("name: " + x.childNodes[i].tagName + "<br/>");
}
document.write("-------------------------------------<br/>");
for (var j = 0; j < x.attributes.length; j++) {
document.write("atrribute: " + x.attributes[j].name + "<br/>");
}
结果:
name: id
name: name
name: author
name: publisher
name: publishdate
name: fee
-------------------------------------
atrribute: type