Hello everyone!
How to make the code less redundant so that namespaces are not strictly defined. I looked at the code examples, but I didn’t manage to implement it, since in these examples it is not very clear how to refer to an element within an element, and then to its attribute.
Please show on several elements in the example in the attachment how to do this.NamespaceKZVUandKVOKS.zip (55.7 KB)
How optimize namespace with XPath.
@sereganator
maybe you can share the exact element name of the two xml structures on where you want to avoid redundancy.
However there are a few options, that can be checked:
-
extract Workflows / seperate XAML for each structure type: KZVU, KVOKS
-
invoking the relevant branch can be done by a switch statement reacting on Root Elementname or defaultnamespace (was answered in one of your others topics)
-
writing generic retrievals with an injected namespace variable like:
- first check which structure type you have to process (see above: switch)
- currentNameSpaceVar = nsVarX
- retrieval: xDoc.Root.Descendants(currentNameSpaceVar +“Elementname”)(0)…
For setting up retrievals with XPath have a look here (was already shared with you in one of your others topic):
Do some exploration of the String.Format method as well. Could be used as e.g:
String.Format("//{0}:ElementName", varCurrentPrefix)
Also have a look on how to formulate the retrieval with local name only:
//*[local-name()='Element1']//*[local-name()='Element2'] .....
how to refer to an element within an element, and then to its attribute.
in general: xDoc.Root(xnsVar + “ParentElementName”)(0).Element(xnsVar + “ChildElementName”).Attribute(“AttributeName”).Value
For example, changed the code from the example to retrieve the attribute that is located:
Parcels ->Parcel → CadastralNumber. But it turns out only to refer to Parcels, when I want to refer to the Parcel Element, and then to the CadastralNumber attribute, then an error occurs.
What am I doing wrong?Example.xaml (11.6 KB)
xDoc.Root.XPathSelectElement("//def:Parcels[1]/def:Parcel",NamespaceMGR).Attribute("CadastralNumber").Value
Thanks, it worked, but how to extract adrs: Note, tried it in different ways?
Is it necessary to create a variable for every value retrieved from a tag?
Since I have 5 columns and 5 extracted values: 1.adrs: Note, 2.Area + Inaccuracy, 3.CadastralNumber, 4. RegNumber 5.RegDate
To then write to the excel file is it enough to use write range?
What does [1] - the first element mean?
Example.xaml (14.8 KB)
I could get to the address in this way:
xDoc.Root.XPathSelectElement(“//def:Parcels[1]/def:Parcel/def:Location/def:Address/adrs:Note”,NamespaceMGR).
I hope it is also correct and will work correctly with a large number of xml files.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.