簡體   English   中英

XDocument后代

[英]XDocument descendants

<?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的sintal之后就停止了。 似乎2個foreach沒有關聯。 每個人都跑

我想得到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();

你在內部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標記,而不是您先前選擇的父節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM