<?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>
I would like to get GOBS-2's Cynthia1. Note there is another Cynthia1 from GOBS-1
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)
{
}
}
}
}
}
the problem is , after it finds Cynthia1 from Gobs-2, the loop goes up to Gobs-1. I think there is a problem with the second foreach for sintal perhaps I should use different thing. I want that after it finds Gobs-2's sintal it just stops looking. it seems the 2 foreach are not related. running on each own
I would like to get GOBS-2's Cynthia1
You can use Linq to get there more precisely:
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();
You have a mistake in the inner foreach loop, you should be iterating over element.Descendants("sintal")
not 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)
{
}
}
}
}
}
}
The reason you are getting the sintal
element from the first groof
tag is that doc.Descendants("sintal")
looks for the first sintal tag in the document, not the parent node you previously selected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With