【问题标题】:XDocument descendantsXDocument 后代
【发布时间】:2014-03-18 23:02:46
【问题描述】:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<kdd>
<Table>
    <robel ID="1">
        <groof NAME="GOBS-1">
            <sintal ID="A">Cynthia1</sintal>
            <sintal ID="B">Sylvia2</sintal>
            <sintal ID="C">Sylvia3</sintal>
            <sintal ID="D">Sylvia4</sintal>
        </groof>
        <groof NAME="GOBS-2">
            <sintal ID="A">Cynthia1</sintal>
            <sintal ID="B">Cynthia2</sintal>
            <sintal ID="C">Cynthia3</sintal>
            <sintal ID="D">Cynthia4</sintal>
        </groof>
        <groof NAME="GOBS-3">
            <sintal ID="A">Daniella1</sintal>
            <sintal ID="B">Daniella2</sintal>
            <sintal ID="C">Daniella3</sintal>
            <sintal ID="D">Daniella4</sintal>
        </groof>
    </robel>
</Table> 
</kdd>

我想要 GOBS-2 的 Cynthia1。注意还有另一个来自 GOBS-1 的 Cynthia1

foreach (XElement element in doc.Descendants("groof"))
                {
                    string mmname = element.Attribute("NAME").Value.ToString();

                        if (mmname == "GOBS-2")
                        {
                            bool found = false; 
                            foreach (XElement element1 in doc.Descendants("sintal"))
                            {

                                if (found == false)
                                {
                                    string CurrentValue = (string)element1;
                                    if ("Cynthia1" == CurrentValue)
                                    {
                                        try
                                        {
                                            //do something
                                            found = true;
                                        }
                                        catch (Exception e)
                                        {
                                        }
                                    }
                                }
                            }
                        }

问题是,在它从 Gobs-2 中找到 Cynthia1 后,循环向上到 Gobs-1。 我认为sintal的第二个foreach有问题 也许我应该使用不同的东西。 我希望它在找到 Gobs-2 的信号后停止查找。似乎 2 foreach 不相关。独立运行

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    我想要 GOBS-2 的 Cynthia1

    您可以使用 Linq 更准确地到达那里:

    XElement cynthia = doc
        .Descendants("groof")
        .Where(g => g.Attribute("NAME").Value == "GOBS-2")
        .Elements("sintal")
        .Where(s => s.Value == "Cynthia1")  // or Attribute("ID") == "A"
        .Single();
    

    【讨论】:

    • 好答案 +1 您可以将 where 移到单曲中以缩短 .Single(s =&gt; s.Value == "Cynthia2");
    • 是的,我知道。但我并不总是觉得这更清楚。在这种情况下,这是一个折腾,我会这样。分别显示所有步骤。
    【解决方案2】:

    您在内部 foreach 循环中有一个错误,您应该迭代 element.Descendants("sintal") 而不是 doc.Descendants("sintal")

    foreach (XElement element in doc.Descendants("groof"))
    {
        string mmname = element.Attribute("NAME").Value.ToString();
    
        if (mmname == "GOBS-2")
        {
            bool found = false; 
            foreach (XElement element1 in element.Descendants("sintal"))
            {
    
                if (found == false)
                {
                    string CurrentValue = (string)element1;
                    if ("Cynthia1" == CurrentValue)
                    {
                        try
                        {
                            //do something
                            found = true;
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
        }
    }
    

    您从第一个groof 标记中获取sintal 元素的原因是doc.Descendants("sintal") 在文档中查找第一个sintal 标记,而不是您之前选择的父节点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      相关资源
      最近更新 更多