在 C# 中使用 XmlDocument 检索值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17396655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:23:56  来源:igfitidea点击:

Using XmlDocument to Retrieve values in c#

c#parsingxmldocument

提问by Kumar

I am using XmlDocument()for parsing a file like **.opf ** for my application of EpubReader.

我正在XmlDocument()为我的 EpubReader 应用程序解析像 **.opf ** 这样的文件。

<item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
<item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />

with

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

These values in same file.

这些值在同一个文件中。

Here I know the value of idwithin the tag of item, after that I want to know the value of the hrefelement.

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

What I have to do is compare the value idrefelement in the itemreftag with element value of idin the tag item. Here I know the idvalue which is W01MB154.

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

Simply, I want to know the next value of idwhich is hrefelement, Using XmlDocument().

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

采纳答案by Vivek Jain

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);
}

Output:

输出:

000Title.html

01MB154.html

000标题.html

01MB154.html

The XML used is:

使用的 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>

Check the structure of the XML document and the XPath expression.

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

回答by Munawar

Following code loads and parses content.opf file without any error.

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

To iterate and compare above xml you may use following code:

要迭代和比较上面的 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);
}