John Ryann
John Ryann

Reputation: 2393

C# reading XML 5 level deep

I need to read sintal id A and Sylvia

<?xml version="1.0" encoding="ISO-8859-1"?>
<kdd>
    <Table>
        <robel ID="1">
            <groof NAME="GOBS-1">
                <sintal ID="A">Sylvia</sintal>
            </groof>
        </robel>
    </Table>
</kdd>

I have tried below and it didnt work. The value for element is null, and after that it exits out.

XDocument doc = XDocument.Load("myname.xml");
foreach (XElement element in doc.Descendants("sintal"))
{
     string my_id = element.Attribute("sintal").Value;
}

Upvotes: 0

Views: 124

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

Your Attribute name is ID not sintal, sintal is your element name. Try this:

 string my_id = (string)element.Attribute("ID");
 string myValue = (string)element;

Also use explicit cast instead of Value property to get the value of the element or attribute.It avoids the NullReferenceException.

Upvotes: 4

Related Questions