更新时间:2023-02-23 08:54:01
您可以从以下代码开始.当我执行它时,我得到正确的输出:
You may start with the following code. When I execute it, I get proper output:
string str = @"<?xml version='1.0' encoding='UTF-8'?><root><items><item id='W01MB154' href='01MB154.html' media-type='application/xhtml+xml' /><item id='W000Title' href='000Title.html' media-type='application/xhtml+xml' /></items><itemrefs><itemref idref='W000Title' /><itemref idref='W01MB154' /></itemrefs></root>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str); // suppose that str string contains the XML data. You may load XML data from a file too.
XmlNodeList itemRefList = xml.GetElementsByTagName("itemref");
foreach (XmlNode xn in itemRefList)
{
XmlNodeList itemList = xml.SelectNodes("//root/items/item[@id='" + xn.Attributes["idref"].Value + "']");
Console.WriteLine(itemList[0].Attributes["href"].Value);
}
输出:
000Title.html
000Title.html
01MB154.html
01MB154.html
使用的XML是:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<items>
<item id='W01MB154' href='01MB154.html' media-type='application/xhtml+xml' />
<item id='W000Title' href='000Title.html' media-type='application/xhtml+xml' />
</items>
<itemrefs>
<itemref idref='W000Title' />
<itemref idref='W01MB154' />
</itemrefs>
</root>
检查XML文档和XPath表达式的结构.
Check the structure of the XML document and the XPath expression.