Extract xml tags from files of different structure

Hello everyone!
I want to extract data from xml files and write to excel. Tags in xml files are different and namespace is different. As a solution, I think to use the if condition, but I don’t know what condition to set.
For example, I want to extract the value from the CadastralNumber tag. Sample files in the archive with the project.
XML_OKS_KVZU.zip (53.5 KB)


Can you try the following?


1 Like

the direct namespace existence is a straight formard approach

  • check default namespace as mentioned by @Yoichi
  • check root element namespace definitions can be done with the attribute property IsNamespaceDeclaration

otherwise you can react on retrieval result counts …Descendendants(…).toList.Count and can also avoid empty result access issues.

Have a look here, maybe you can modify for your scenario

In case of you need more help, then just provide us the more detailed requirements of your case. Thanks

Thanks a lot, this way works!

1 Like

Will this method work if the tags in the xml files are different?


Will this method work if the tags in the xml files are different?

  • checking the attributes on its IsNamespaceDeclaration flag is available for all xelements
  • xDOC.Root.Descendants(“FullQualifiedelementname”).Count is independent to rootname and dependend to the assigned Elementname. But if the element is not present in the XML then it will return 0 and will not throw an exception (e.g. Null, not set to reference)

In your case also the check can be changed into a switch approach:

  • xDoc.Root.Name.LocalName would return KVOKS or KVZU
  • define the different cases for the different XML types (KVOKS or KVZU)
  • use the default case for handling the not specified cases

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.