XML data manipulation with namespace

Hi Team,

I am working with XML data where i need to search plan id if plan id found i want to copy to new xml file from to i am able to get the specific plan id but it’s getting namespace as well i dont want to keep namespace
below query i tried
xmlDoc.Descendants(“{ns}PlanDefinition”) _
.Where(Function(x) x.Element(“{ns}PlanUpdate”) IsNot NothingAndAlso _
x.Element(“{”).Attribute(“PlanID”) IsNot Nothing AndAlso _
planIdsToSearch.Contains(x.Element(“{}PlanUpdate”).Attribute(“PlanID”).Value)) _
.ToList()

test.xml (11.4 KB)

would appreciate if anyone can help

Have a look here:
XML Extraction - Handling Namespaces (Tableau, Soap, OASIS types) - Help / Community - UiPath Community Forum

UPD1 - XNamespace Variable Usage

When ns is variable of DataType XNamespace and is holding the Namespace like:
ns = xmlDox.Root.Name.Namespace
then:

Descendants(“{ns}PlanDefinition”)

has to be changed to:
Descendants(ns + "PlanDefinition")

Also have a look at the following:

Working with XPath and local name:

xDoc.Root.XPathSelectElements("//*[local-name() = 'PlanDefinition']")....

→ import System.Xml.Xpath

Also we can rewrite the LINQ to:

(From xe In xmlDoc.Root.Descendants()
Where xe.Name.LocalName.equals("PlanDefinition")
Let pu =  xe.Elements().First()
Where pu.Name.LocalName.Equals("PlanUpdate")
Where pu.Attributes().Any(Function (a) a.Name.LocalName.Equals("PlanID"))
Let pi = pu.Attribute("PlanID").Value
Where planIdsToSearch.Contains(pi)
Select r = xe).toList

I have tried but its keeping namespace as welln i want to get from PlanDefinition to PlanDefinition and write into new xml file

which one?

however let scope 1 Topic = 1 use case

We had shown (and also proven at our end) the LINQ, so we can use/avoid the namespace part for the filtering, which was the origin question

The one you have shared its giving me with namespace i want to copy from plandefinition to plandefinition please check the file i have shared

yes which is the corrext behaviour.

When your requirement is about

  • filter Elements based on the PlanIDs from planIdsToSearch Collection
  • create a new XML based on the search results

Then:

  • realign and respecify your use case and requirements

As mentioned:

Other researchers can faster find solutions for their similar use cases, when 1 Topic = 1 use case

However for creating new XMLs you have severl options availaible

  • Document namespace removal
  • Fragment usage and adding the results

Thanks it got resolved

1 Like

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