使用XmlDocument在c#中检索值

我正在使用XmlDocument()为我的EpubReader应用程序解析文件,如* .opf *

   

    

这些值在同一个文件中..

在这里我知道id的值在item的标签中,之后我想知道href元素的值。

我要做的是将itemref标记中的值idref elemnt与标记id的元素值进行比较。 在这里我知道id值是W01MB154

简单地说,我想知道id的下一个值是href元素,使用XmlDocument(),

如果有人意识到这种情况,请帮帮我。

谢谢,

您可以从以下代码开始。 当我执行它时,我得到适当的输出:

 string str = @""; 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

01MB154.html

使用的XML是:

            

检查XML文档的结构和XPath表达式。

下面的代码加载并解析content.opf文件没有任何错误。

要迭代和比较上面的xml,您可以使用以下代码:

 try { XmlDocument xDoc = new XmlDocument(); xDoc.Load("content.opf"); XmlNodeList items = xDoc.GetElementsByTagName("item"); foreach (XmlNode xItem in items) { string id = xItem.Attributes["id"].Value; string href= xItem.Attributes["href"].Value; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); }