是否存在XML标记?

时间:2013-07-13 06:54:17

标签: c# xml c#-4.0

如何检查C#中是否存在xml标记? 我的Xml

<name>
  <firstname>John</firstname> 
  <lastname>cena</lastname> 
  <job>Administrator</job> 
  <location>sunnyvale</location> 
  <age>19</age> 
</name>

<name>
  <firstname>mark</firstname> 
  <job>Agent</job> 
  <location>Bangalore</location> 
  <age>22</age> 
</name>

lastname标签不存在,用于标记如何签入C#?

1 个答案:

答案 0 :(得分:1)

string xml = @"
    <name>
        <firstname>mark</firstname> 
        <job>Agent</job> 
        <location>Bangalore</location> 
        <age>22</age> 
    </name>";

var doc = XDocument.Parse(xml);
bool isLastNameExists = doc.Descendants("name").Elements("lastname").Any();