XPath get attribute value

Hello!
How do I get an attribute through XPath? (expression must be on one line)
Here is my expression xDoc.Root.XPathSelectElement (“// defKVZU: Parcels [1] / defKVZU: *”, NamespaceMGR).
Attribute path:
/KVZU [@xmlns=“urn://x-artefacts-rosreestr-ru/outgoing/kvzu/7.0.1"”"<Parcels/Parcel [@CadastralNumber=“29:20<20101:11”>@CadastralNumber

How do I get an attribute through XPath - Google Search

@sereganator
kindly note the return on XPathSelectElement is a XElement
you can have a check on following activity:

maybe not working directly with the namespace, but maybe can be tricked with
{NamespaceID}elementname

However we would suggest to work on Xelement base and do the retrieval/evaluation later on Attribute level. Also we can do a filtering

I don’t really understand how to specify the namespace. Through local name?
I just want the attribute to be retrieved in one variable, then to create a protective string type and check for the existence of the tag, if there is no tag, then write an empty value. As you already helped in this thread:

@sereganator
lets sort it out one by one with some patience:
Using XPathSelectElement will return a XElement

Using with this method a XPath statement with the intention to retrieve an attribute value will result into NULL / fail.

Is it more clear now? If yes then we can go the next step

Yes, I understand, if there is no element, then the error Object reference not set to an instance of an object.

Yes, I hope we do mean the same: The yellow highlighted is the trigger:

so as mentioned above:
var1 = returned XElement from XPathSelectElement Method
var2 = var.Attribute(“Name”).Value

What i s the reason at your end that you cannot work with these two variables?

Data type of var1 and var2 XElement?
Because an error occurs
cannot be set to type system.string to type system.xml.linq.xelement.
cadNumb=xDoc.Root.XPathSelectElement(“//defKVZU:Parcels[1]/defKVZU:*”,NamespaceMGR)
cadNumbAttr=cadNumb.Attribute(“CadastralNumber”).Value

it is transparent and derivable

var1 (as mentioned above in the doculink) - DataType: XElement (have a look on the other implementations you had done so far)

var2: Datatype: String
Docu reference link here:

Works, changed the data type to String.
Is it possible to check for the presence of a tag in this variable? Such an expression throws an error if (isNothing (cadNumb.Attribute (“CadastralNumber”)), “Empty”, cadNumb.Value.ToString) - because it considers not an attribute, completely a tag.