I am trying to loop through a directory to search all xml files. I got this accomplish by doing Directory.EnumerateFiles
and by finding every .xml type. The thing is now, there are some random xml files that are empty with no information written in them. I want to check for all files that do not have any information in them.
Code
foreach (string file in Directory.EnumerateFiles("/xmlFiles", "*.xml", SearchOption.AllDirectories))
{
//Find the XML File
XDocument doc = XDocument.Load(file);
//Check if the XDocument doc has no root element
if (doc == null)
{
Console.WriteLine(File + " has NO root element");
}
else
{
Console.WriteLine(File + " has a root element");
}
}
I tried doing a if else statement comparing the file to null after parsing the xml file but I get a error when loading my file before I can compare it.
Error: System.Xml.XmlException: 'Root element is missing.'