2014-02-28 43 views
0

我需要阅读sintal ID A和西尔维亚C#读取XML 5层深

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

下面我试图和它没有工作。元素的值为null,之后退出。

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

它以什么方式不起作用? –

+0

您应该只能使用元素的XPath。 (如果您知道文档结构,则无需循环) –

回答

4

您的属性名称不IDsintalsintal是你元素名。试试这个:

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

还可以使用明确投而不是Value属性来获取元素的值或attribute.It避免NullReferenceException