【问题标题】:To fetch the attributes from the NodeList of XmlDocument?从 XmlDocument 的 NodeList 中获取属性?
【发布时间】:2009-04-24 07:53:27
【问题描述】:

我有一部分 XmlDocument Example.xml,如下所示:

<rapaine dotoc="palin" domap="rattmin">
  <derif meet="local" />
  <derif meet="intro" />
.
.
.
</rapaine>

在这里,我正在创建一个节点列表并获取元素 raplin 以获取其属性。

现在我想确定属性 'dotoc' 和 'domap' 是否是 rapaine 的属性,其各自的值始终是固定的。然后只有我可以使用其属性 'meet' 访问 childNodes 'deriff'。这里的值只会改变。
我已经写了一部分代码没有编译错误但是在调试时我发现它没有进入for循环来检查它的属性和子节点。

XmlNodeList listOfSpineRootNodes = opfXmlDoc.GetElementsByTagName("rapine");
for (int x = 0; x < listOfSpineRootNodes.Count; x++)
  {
    XmlAttributeCollection spineAttributes = listOfSpineRootNodes[x].Attributes;
    string id = spineAttributes[0].Value;
    if (spineAttributes != null)
    {
      XmlNode attrToc = spineAttributes.GetNamedItem("dotoc");
      XmlNode attrPageMap = spineAttributes.GetNamedItem("domap");
      if (attrToc.Value == "palin" && attrPageMap.Value == "rattmine")
      {
        if (listOfSpineRootNodes != null)
        {
          foreach (XmlNode spineNodeRoot in listOfSpineRootNodes)
          {
            XmlNodeList listOfSpineItemNodes = spineNodeRoot.ChildNodes;
            if (listOfSpineItemNodes != null)
            {
              foreach (XmlNode spineItemNode in listOfSpineItemNodes)
              {
                if (spineItemNode.NodeType == XmlNodeType.Element
                  && spineItemNode.Name == "derif")
                {
                  XmlAttributeCollection spineItemAttributes = spineItemNode.Attributes;

                  if (spineItemAttributes != null)
                  {
                    XmlNode attrIdRef = spineItemAttributes.GetNamedItem("meet");
                    if (attrIdRef != null)
                    {
                      spineListOfSmilFiles.Add(attrIdRef.Value);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

你能告诉我哪里出错了吗.. 谢谢....

【问题讨论】:

    标签: c# xml xmldocument


    【解决方案1】:

    您可以使用 XPath 使用以下代码执行此操作。由于 XPath 是一种专门为查询 XML 文档而设计的语言,因此您应该考虑学习它。大多数新手都喜欢在W3schools 开始学习。

    代码如下:

    XmlNodeList meetList = opfXmlDoc.SelectNodes("/rapaine[(@dotoc = 'palin') and (@domap = 'rattmin')]/derif/@meet")
    if (meetList.Count > 0)
    {
      foreach (XmlNode meet in meetList)
      {
        spineListOfSmilFiles.Add(meet.Value);
      }
    }
    

    XPath 表达式供您参考:

    /rapaine[(@dotoc = 'palin') and (@domap = 'rattmin')]/derif/@meet
    

    可以解释为:

    a) 查找所有rapaine 根级元素,这些元素具有属性dotoc 的值为“palin”,并且属性domap 的值为“rattmin”。

    b) 在这些 rapaine 元素中,找到所有 derif 子元素。

    c) 在这些 derif 元素中,检索所有 meet 属性。

    请注意代码变得多么简洁。

    【讨论】:

      【解决方案2】:

      你不能用一个简单的 XPath 表达式来解决这个问题吗?

      所有带有条件的嵌套循环都是自找麻烦。

      【讨论】:

      • 不,我不能使用 XPath。老实说,我不知道如何在这里使用 XPath。谢谢
      • 是的,您可以为此使用 XPath,但您必须对其进行研究。
      • 我并不是说你的整个问题都可以用一个表达式来解决,你可能想把它分解成更小的部分,并且可能需要一些迭代来完成它,但总的来说我'我相信这是可能的。
      【解决方案3】:

      根据您使用的 .NET 版本,LINQ 可能会简化此操作。

      【讨论】:

        猜你喜欢
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        相关资源
        最近更新 更多