gpt4 book ai didi

c# - XDocument后代

转载 作者:数据小太阳 更新时间:2023-10-29 01:53:16 24 4
gpt4 key购买 nike

<?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 不相关。各自奔跑

最佳答案

I would like to get GOBS-2's 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();

关于c# - XDocument后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com