2012-06-27 38 views
0
XElement MyFamily = new XElement("MyFamily", 
           new XElement("Parents", 
            new XElement("Father", "Anjappan", 
             new XAttribute("Id", "AA1234")), 
            new XElement("Mother", "Thaiyamuthu", 
             new XAttribute("Id", "AA4567"))), 
           new XElement("Brothers", "Senthil,Saravanan,Sathish"), 
           new XElement("Systers", "Povunamma,Pazhaniyamma,Sangeetha")); 
     MyFamily.Save(@System.AppDomain.CurrentDomain.BaseDirectory + "MyFamily_RemoveElement.xml"); 

在這裏,我想父親值( 「Anjappan」),並使用LINQ to XML概念標識值( 「AA12345」)使用LINQ to XML概念得到XML元素的值。我應該爲此做些什麼。如何在WPF

回答

0

使用Descendants獲取節點,Attributes獲取屬性,Value獲取元素/節點值。

var fatherNode = MyFamily.Descendants("Father"); //Father Nodes 
    var fatherId = fatherNode.Attributes("Id").First().Value; // "AA1234") 
    var farthName = fatherNode.First().Value; //"Anjappan" 
1

試試這個,

var element = MyFamily.Descendants("Father") 
        .Where(
        p => p.Value == "Anjappan" 
        && p.Attribute("Id").Value == "AA1234" 
        ).FirstOrDefault();