ms自带的例子:
using System;
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{ XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples"); //Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList){
Console.WriteLine(isbn.Value);
} }}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;int main()
{
XmlDocument* doc = new XmlDocument();
doc -> Load(S"booksort.xml"); // Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc -> NameTable);
nsmgr -> AddNamespace(S"bk", S"urn:samples"); // Select and display the value of all the ISBN attributes.
XmlNodeList * nodeList;
XmlElement * root = doc -> DocumentElement;
nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);
IEnumerator* myEnum = nodeList->GetEnumerator();
while (myEnum->MoveNext())
{
XmlNode* isbn = __try_cast<XmlNode*>(myEnum->Current);
Console::WriteLine(isbn -> Value);
}
}
该示例使用文件 booksort.xml 作为输入。
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
可以成功,但是在nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);变成
nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN'"+"1-861002-30-1"+"'", nsmgr);就出错,小弟新来的,高手指教。
using System;
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{ XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml"); //Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples"); //Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList){
Console.WriteLine(isbn.Value);
} }}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Collections;int main()
{
XmlDocument* doc = new XmlDocument();
doc -> Load(S"booksort.xml"); // Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager* nsmgr = new XmlNamespaceManager(doc -> NameTable);
nsmgr -> AddNamespace(S"bk", S"urn:samples"); // Select and display the value of all the ISBN attributes.
XmlNodeList * nodeList;
XmlElement * root = doc -> DocumentElement;
nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);
IEnumerator* myEnum = nodeList->GetEnumerator();
while (myEnum->MoveNext())
{
XmlNode* isbn = __try_cast<XmlNode*>(myEnum->Current);
Console::WriteLine(isbn -> Value);
}
}
该示例使用文件 booksort.xml 作为输入。
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
可以成功,但是在nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN", nsmgr);变成
nodeList = root -> SelectNodes(S"/bookstore/book/@bk:ISBN'"+"1-861002-30-1"+"'", nsmgr);就出错,小弟新来的,高手指教。
try
{
xmlDoc.Load(FilePath_Style);//FilePath_Style为文件路径
}
catch
{
return;
}
try
{
xmlDoc.Load("c:\\1.xml");//FilePath_Style为文件路径
}
catch
{
return;
}
XmlNode root = xmlDoc.ChildNodes[2];
if (root.HasChildNodes)
{ for (int row_i=0; row_i<root.ChildNodes.Count;row_i++)
{
if(root.ChildNodes[row_i].LocalName=="book")
{
for(int i = 0;i<root.ChildNodes[row_i].Attributes.Count;i++)
{
Response.Write(root.ChildNodes[row_i].Attributes[i].LocalName);//返回每一个属性的名称
Response.Write(root.ChildNodes[row_i].Attributes[i].InnerText);//返回每一个属性值
}
XmlNodeList nodeList_Table;
XmlNode root_TabStyle = xmlDoc.DocumentElement;
nodeList_Table = root_TabStyle.SelectNodes("descendant::title");
foreach(XmlNode xmlnode_RowCount in nodeList_Table)
{
string s = xmlnode_RowCount.InnerText; //可以返回title结点中间的InnerText内容
}
}
}
}==========记得今后发贴要给分,不然没人回你的贴这里的分每天只要你登陆就会给你加,何必那么吝啬对不
XmlNode node = ....
XmlNode selectNode = node.SelectSingleNode( "/节点 [@属性 = 属性值 ]" );//单选
XmlNodeList selectNodes = node.SelectNodes( "/节点 [@属性 = 属性值 ]" );//多选