/* using System; using System.Linq; using System.Xml.Linq; */
string xml = @" <root> <one>this one</one> <two>this two</two> <three> this one <three_5>and a half</three_5> </three> </root> ";
XDocument document = XDocument.Parse( xml );
var assertions = from n in document.Descendants( "root" )
select new
{
HasOne = n.Element( "one" ),
HasTwo = n.Element( "two" ),
HasThree = n.Element( "three" ),
HasTen = n.Element( "ten" )
};
//Verify that there is one root element: if ( assertions.Count() == 1 ) { var test = assertions.First();
Console.WriteLine( test.HasOne != null );
Console.WriteLine( test.HasTwo != null );
//Search for nested element:
if ( test.HasThree != null )
{
Console.WriteLine( test.HasThree.Element( "three_5" ) != null );
}
Console.WriteLine( test.HasTen != null );
}
/*
Console output:
True True True False
*/