使用XmlDocument在c#中检索值

3
我正在使用XmlDocument()来解析类似于**.opf**的文件,以用于我的EpubReader应用程序。
<item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
<item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />

使用

  <itemref idref="W000Title" />
  <itemref idref="W01MB154" />

这些值在同一个文件中。

我知道item标签中id的值,然后想知道href元素的值。

我需要比较itemref标签中idref元素的值与item标签中id元素的值。我已经知道id的值为W01MB154

简单来说,我想要使用XmlDocument()方法找到下一个id元素的href值。


你能发布完整的XML文件或至少是包围item标签的其他标签吗?必须有单个根元素,但在上面的示例中有更多。 - Munawar
抱歉要说,XMlDocument() 在 Store 应用程序中有很大的变化。我以控制台应用程序的形式检查了您的代码,看起来不错。我没有提到是 Store 应用程序是我的错误。对此我表示歉意,我将改变问题。谢谢。 - Kumar
2个回答

6

以下代码可以成功加载和解析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);
}

这是我的文件[http://sdrv.ms/14HtQdi]。 现在我正在这样做... 我没有使用根节点。 XmlNodeList itemref = xmlDoc1.GetElementsByTagName("itemref"); foreach (XmlElement idref in itemref) { if (idref.Attributes.Count > 0) { XmlAttribute Str1 = idref.GetAttributeNode("idref"); ids1 = Str1.Value; for (int i = 0; i < itemref.Count; i++) { ids1.ToCharArray(); } } }看了你的代码后,我打算对它进行更改。 你能帮我吗? 因为我是新手, 谢谢。 - Kumar
当我尝试使用你的代码片段时,我遇到了一个错误:Windows.Data.Xml.Dom.XmlNamedNodeMap.this[int]的最佳重载方法匹配有一些无效参数。在此之前,我没有得到XmlNode,而是使用了IXmlNode - Kumar
XmlDocument()中没有Load定义。请您检查一下这个链接:[http://sdrv.ms/1b1lzpf]。 如果我使用.LoadXml(),那么在xItem.Attributes["id"].Value;处会出现错误:**Cannot apply indexing with [] to an expression of type 'method group'**。 - Kumar
你需要添加对 System.Xml 的引用;或者像这样使用完全限定名称来访问 XmlDocument 类。System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); - Munawar
唯一的问题是,您正在开发一个店铺应用程序,其中引用了 Window.Data.Xml 命名空间而不是 System.Xml。 您没有在任何地方指定您正在开发商店应用程序,并且希望以某种不同的方式处理 xml。无论如何,如果只是使用 XmlDocument 的完全限定名称,它应该能够为您工作(如我上次评论中所提到的)。 - Munawar
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/32697/discussion-between-kumar-and-needo - Kumar

6
您可以从以下代码开始。当我执行它时,我得到正确的输出:
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

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表达式。

当我尝试使用你的代码片段时,我遇到了一个错误。"Windows.Data.Xml.Dom.XmlNamedNodeMap.this[int]"的最佳重载方法匹配存在一些无效参数。在此之前,我没有得到XmlNode,而是使用IXmlNode,可能我正在开发Windows Metro应用程序,这就是为什么它不能工作在XmlNode上。 我需要您的建议,我将使用Linq to SQL还是XDocument()。 您能否建议哪种方式适合我的情况或是否有其他解决方案? - Kumar
是的,我只使用了 GetElementsByTagName("itemref");。 在这里我已经有了结构。我知道列表中 id 的值。 是否有任何属性可以获取下一个元素的值,该值为 XmlDocument() 中的 href。给我一些想法。 谢谢 - Kumar
@kumar,请查看我的编辑。在控制台应用程序中,它对我很好并给了我所需的输出。请检查XML文档的结构和XPath表达式。 - Vivek Jain
你尝试过使用XDocument或者LINQ to XML吗? - Vivek Jain
不,还没有。我打算开始做这件事。 如果您不介意,您能给我一些想法吗? - Kumar
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接