With XDocument you can easily query and check if the numbers exist in your XML.
// XML content
string xml = "<Numbers>" +
"<Num>1</Num>" +
"<Num>2</Num>" +
"<Num>3</Num>" +
"<Num>4</Num>" +
"<Num>5</Num>" +
"</Numbers>";
load string into XDocument. If you read from file use XDocument.Load(pathname)
instead.
var doc = XDocument.Parse(xml);
check if the random number exists.
Take all elements from Numbers
Select the value inside it
Convert the results to string array
Check if the array contains the random number
int random = 3;
bool exists = doc.XPathSelectElement("Numbers")
.Elements()
.Select(x => x.Value)
.ToArray()
.Contains(random.ToString());
Make sure to include those namespaces
using System.Xml.Linq;
using System.Xml.XPath;