Question

I have the following snippet of XML (Zimbra SOAP API if anyone cares, but it is not important)

<appt id="266" uid="bf177af2-2875-447a-8e74-d9bc8c108611" nextAlarm="1409547300000" d="1380213518000" rev="63" s="0" l="10" xmlns="urn:zimbraMail">
  <inv id="265" seq="3" compNum="0" type="appt">

I have an XmlElement which contains this snippet. I have set up a namespace manager etc, as follows

XmlNamespaceManager ns = new XmlNamespaceManager(element.OwnerDocument.NameTable);
ns.AddNamespace("prefix", "urn:zimbraMail");

The following code

element.SelectSingleNode("//prefix:" + "appt/@id", ns).Value

returns 266 as expected

but I cannot manage to select appt, appt/inv or anything else, except for basically the attributes of appt. Here are a few of the queries I have tried. Some return null, some return bad format (as I was thrashing around)

element.SelectSingleNode("//prefix:" + "appt/inv", ns).Value
element.SelectSingleNode("//prefix:" + "/appt/inv", ns).Value
element.SelectSingleNode("//prefix:" + "/inv", ns).Value
element.SelectSingleNode("//prefix:" + "appt", ns).Value

I've also tried not including the prefixes etc. Any help? Part of the issue may be that OwnerDoc is a SOAP envelope, which could be screwing up the namespaces, but then why would the first query work? (Incidentally, the first query works even without using the prefix/namespace logic)

Was it helpful?

Solution

You need to qualify all elements, not just the first one. So:

//prefix:appt/inv

Should be:

//prefix:appt/prefix:inv

The bad format exception is due to the following, because element names can't start with /:

//prefix:/appt/inv
//prefix:/inv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top