0

I need to validate a selected Xml file using Xdocument without Xsd. I have a file named "Cheker" and the file to check. for example i need to compare the hierarchy ,and how much elements by name from the checker file. if i have in the "checker" file 3 page i need to chek there is no more in the selected file. I tried with a array but is to much complicated like this thanks!!

            XElement pageElement = metadataFile.Root.Element("Pages");
            int cntPage = ((IEnumerable<XElement>)pageElement.Elements()).Count();
            if (cntPage < 1 || cntPage > 3) errorDetails += "Number of Pages wrong!!";
Nejc Galof
  • 2,538
  • 3
  • 31
  • 70
  • 1
    It is not too complicate, IMO. What way you expect to more simpler? – NoName Mar 05 '16 at 10:15
  • what the command for read the Xdocument and copy with the hierarchy into array,for checking the hierarchy and count specific elements? – Colombus Mar 05 '16 at 11:51
  • It's probably helpful to show a short example of the XML file you're examining, and the code you used to construct the `metadataFile` element. Please consider editing your question to show these things. – O. Jones Mar 05 '16 at 13:01
  • this is a example of md file – Colombus Mar 06 '16 at 15:06

1 Answers1

0

Elements() already returns IEnumerabl<XElement>. So the explicit cast on the second line of your code is unnecessary :

int cntPage = pageElement.Elements().Count();

Which style to use is a matter of preference here, but the entire code snippet can be re-written to be as follow :

int cntPage = metadataFile.Root
                          .Element("Pages")
                          .Elements()
                          .Count();
if (cntPage < 1 || cntPage > 3) 
    errorDetails += "Number of Pages wrong!!";
har07
  • 88,338
  • 12
  • 84
  • 137