简体   繁体   中英

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM