Extract data xml file with namespace

I am trying to read data from an xml file, but an “System.NullReferenceException: Object reference not set to an instance of an object” exception occurs.
Please tell me how to modify the code to read it using the namespace.
An example of how I access the desired attribute:


have a first look here:

The example has one namespace, and I have many. Create a separate variable for each namespace?

yes we would create for each namespace which we are using in the retrieval a XNamespace variable.

Have also a look here on a xpath approach and configuration via datatable. Maybe you prefer such an approach for your case

For example, how to read namespaces if they are in this form? 8 namespace

Please show how to extract using this xml file as an example:
<Parcel CadastralNumber = “53: 14: 221323432” and <adrs: Note> Street, city </ adrs: Note>.
I’ll do the rest according to the sample.example.xml (20.8 KB)

not sure what is the question about

with XNameSpaceVar we can implement the retrieval as shown in the demo
in case of we have many namespaces to handle then we can think about on easy get generated it e.g.:

  • config tables
  • dictionary(Of String, XNamespace)

as we do use the XNamespacevar within the retrieval like xDocVar.Root.Element(XnamespaceVar+“ElementLocalName”) writing of long namespace ids can be avoided.

Also have look here, what Microsoft is stating to the topic:

should work within the same manner:
Document Default namespace is: xmlns="urn://x-artefacts-rosreestr-ru/outgoing/kvzu/7.0.1

the others can be identified on the prefix

Sample code trying to read <adrs: Region> 12 </ adrs: Region>
But it doesn’t work. What am I doing wrong?
XMLNamespace.zip (17.5 KB) example.xml (20.8 KB)


the provided XML is not valid, so just do a correction of it and share it again. So we can use it for sample code if needed

the statement xDoc.Root.Element(nsAdrs+“region”) looks for an element that is directly under the root element with the name region.

Comparing with the screenshot the path is wrong, the element name is case sensitive and also not correct.

give a try on
looking for 1st adrs:Region Element as a descendant child

or use a more detailed correct path to this element

Thank you so much.
This method helped to extract data from the adrs namespace.
But there is one more small question.
Items Tagged
<Area 240390 /Area>
<Unit 055 /Unit>
Inaccuracy 172 /Inaccuracy
<RegNumber 53-01 / 14-04 / 2002-354 /RegNumber>
Are not in the namespace but cannot retrieve them.
How to do it? Tried to extract with construct var.Element (“Element”).Element (“Element”).Attribute (“Attribute”)

these elements are running under the default namespace. Same procedure:

  • define a XNamespace Variable, DefaultValue, urn://x-artefacts-rosreestr-ru/outgoing/kvzu/7.0.1, varname e.g. xnsDEF
  • use xnsDef within the retrieval similar to above

Checking if the document runs over a default namespace can be done as following:

set breakpoint after serializing xml, debug, watch/immediate panel
Statement: xDocVar.Root.Name.toString

the output will show you the localname only (no DefaultNamespace) Or Namespace+localname

I’ve already done that. The wrong data is being output.
I want to get <Parcel CadastralNumber = “53: 14: 221323432”
but I get <CadastralNumber 53: 14: 0 CadastralNumber> Why?XMLNamespace2.zip (17.8 KB) example.xml (20.8 KB)

ensure that element Parcel is fetched and retrieve from it the value from Attribute: CadastralNumber

try: xDoc.Root.Descendants(nsKZVU+“Parcel”)(0)
and retrieve value from attribute: YourXlementVar.Attribute("CadastralNumber”).Value.toString

As we found in your code: xDoc.Root.Descendants(nsKZVU+“CadastralNumber”)(0) we do see a link to the wrong taken output as it fetchs the first CadastralNumber

1 Like

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