Hello!
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:
[xdata.Element(“KVZU”).Element(“Parcels”).Element(“Parcel”).Attribute(“CadastralNumber”)
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)
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:
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)
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
</Area
<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”)
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