Charles Morrison
Charles Morrison

Reputation: 428

adding namespace to xpath

I have classes full of xpaths that now needs a specfic namespace. How can I accomplish this? What would be the easiest way to refactor this code? find replace?

What would be the best way to grab specific nodes in the future, if I don't know the namespaces ahead of time?

susr1 = root.SelectSingleNode("/PurchaseOrderRequest/DocumentHeader/DocumentInformation/DocumentIdentification/Type");

susr1 = root.SelectSingleNode("/PurchaseOrderRequest/ssdh:DocumentHeader/ssdh:DocumentInformation/ssdh:DocumentIdentification/ssdh:Type");

Upvotes: 1

Views: 89

Answers (1)

Chuck Savage
Chuck Savage

Reputation: 11945

You can try this XML Library. Use XPathElement(). This is a .Net 3.5 solution if you can use it.

XElement root = XElement.Load(file) or .Parse(string)

Then either of your xpath's should work as long as there are not conflicting nodes with the same name (but different namespaces).

XElement susr1 = root.XPathElement("/PurchaseOrderRequest/DocumentHeader/DocumentInformation/DocumentIdentification/Type");

XElement susr1 = root.XPathElement("/PurchaseOrderRequest/ssdh:DocumentHeader/ssdh:DocumentInformation/ssdh:DocumentIdentification/ssdh:Type");

The library should figure out the namespace for you. I cannot test it without example xml to be sure.

Upvotes: 1

Related Questions