XDocument attribute value

本文关键字:value attribute XDocument | 更新日期: 2023-09-27 17:50:45

<?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>

如何得到Sylvia?

XDocument doc = XDocument.Load("x.xml");
foreach (XElement element1 in doc.Descendants("sintal"))
{ 
    if (element1.Attribute("ID=""A""").Value == c.name)
     { 
         //do I get Syliva here?
      }
}

XDocument attribute value

只需使用显式强制转换并将元素强制转换为string:

foreach (XElement element1 in doc.Descendants("sintal"))
{ 
    string currentValue = (string)element1;
}

p。S.为了获得属性的值,您需要使用它的名称。在这种情况下,属性名称是ID而不是ID=""A"""A"是属性的值。