<?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。 我认为第二个foreach对sintal存在问题, 也许我应该使用不同的东西。 我希望在找到Gobs-2的sintal后它就停止查找。似乎这两个foreach没有关联,各自运行。
.Single(s => s.Value == "Cynthia2");
的长度。 - crthompson