Kevin S. Miller
Kevin S. Miller

Reputation: 960

XPath multiple, duplicate anonymous namespaces

I need to process the data in an XML file similar to this:

 <?xml version="1.0" encoding="utf-8"?>
    <bpr:release xmlns:bpr="http://www.bp.co.uk/product/release">
        <bpr:contents>
            <process id="1" name="Process 1" xmlns="http://www.bp.co.uk/product/process">
                <process name="Process 1"></process>
            </process>
            <process id="2" name="Process 2" xmlns="http://www.bp.co.uk/product/process">
                <process name="Process 1"></process>
            </process>
            <object id="1" name="Object 1" xmlns="http://www.bp.co.uk/product/process">
                <process name="Object 1" ></process>
            </object>
            <work-queue id="1" name="Queue1" xmlns="http://www.bp.co.uk/product/work-queue"/>
            <process-group id="1" xmlns="http://www.bp.co.uk/product/process-group">
                <members>
                    <process id="1"/>
                    <process id="2"/>
                </members>
            </process-group>
            <object-group id="1" xmlns="http://www.bp.co.uk/product/object-group">
                <members>
                    <object id="1"/>
                    <object id="2"/>
                </members>
            </object-group>
        </bpr:contents>
    </bpr:release>

For example, I need to extract the /bpr:release/bpr:contents/process/process nodes for analysis, but I get nulls or this error:

System.Xml.XPath.XPathException
  HResult=0x80131943
  Message=Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
  Source=System.Xml

I see that the file has multiple anonymous namespaces, and there is the same namespace used twice. I've tried various combinations, for example:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlPath);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("bpr", "http://www.bp.co.uk/product/release");
nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpo", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpwq", "http://www.bp.co.uk/product/work-queue");
nsmgr.AddNamespace("bppg", "http://www.bp.co.uk/product/process-group");
nsmgr.AddNamespace("bpog", "http://www.bp.co.uk/product/object-group");
XmlNodeList p;
XmlNodeList r;
try { r = xmlDoc.SelectNodes("/bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("//bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("/release"); } catch { }
try { r = xmlDoc.SelectNodes("//release"); } catch { }
try { r = xmlDoc.SelectNodes("release"); } catch { }
try { p = xmlDoc.SelectNodes("process"); } catch { }
try { p = xmlDoc.SelectNodes("bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("//process"); } catch { }
try { p = xmlDoc.SelectNodes("//bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpr:release/bpr:contents/process/process"); } catch { }

What is the correct XPath to get each of the child nodes of bpr:release, bpr:contents, process/process, object/process, etc.? How to use the XMLNamespaceManager for this?

Upvotes: 1

Views: 128

Answers (1)

kjhughes
kjhughes

Reputation: 111501

You're adding namespace prefix declarations to your XmlNamespaceManager, but you're not using nsmgr in your calls to SelectNodes().

try { r = xmlDoc.SelectNodes("/bpr:release", nsmgr); } catch { }
                                           ^^^^^^^

Fix up the other calls accordingly.

Note also that your first example XPath,

/bpr:release/bpr:contents/process/process

should be

/bpr:release/bpr:contents/bpp:process/bpp:process

given your declaration:

nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");

Finally, you should remove duplicate prefix declarations (e.g. bpp/bpo) in some of the AddNamespace() calls.

See also

Upvotes: 3

Related Questions