S
S
Stels0072018-06-30 16:47:51
C++ / C#
Stels007, 2018-06-30 16:47:51

C# XML How to check if a node exists?

Hello! I can not find on the Internet how to check the existence of a node?

<head>
 <block1></block1>
 <block2></block2>
 <block3></block3>
</head>

For example, how to find out if the block3 element exists?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2018-06-30
@Stels007

using System.Xml;

var xml = @"<head>
 <block1></block1>
 <block2></block2>
 <block3></block3>
</head>";

var doc = new XmlDocument();  
doc.LoadXml(xml);

var node = doc.SelectSingleNode("/head/block3");

if (node != null)
{
  Console.WriteLine("Узел существует!");
}
else
{
  Console.WriteLine("Узел не найден...");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question