Problems with XPath

B

bbb

Hi,
I need to get some values from the OAGIS xml( giving valid part for
simlicity):
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <ShowInvoice environment="" lang="en-US" revision="8.0"
xmlns="http://www.openapplications.org/oagis" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
www.openapplications.org/oagis C:\OAGIS8.0\OAGIS\BODs
\ShowInvoice.xsd">
- <ApplicationArea>
- <Sender>
<LogicalId>LID</LogicalId>
<Confirmation>Always</Confirmation>
<AuthorizationId>AID</AuthorizationId>
</Sender>
<CreationDateTime>2007-01-05T17:09:56Z</CreationDateTime>
<BODId>12345678</BODId>
<UserArea />
</ApplicationArea>
- <DataArea>
<Show confirm="Always" />
- <Invoice>
- <Header>
- <DocumentIds>
- <DocumentId>
<Id>123456789</Id>
</DocumentId>
</DocumentIds>
<Note>Note</Note>
<TotalAmount currency="USD">10000.0</TotalAmount>
<PaymentTerms />
<Type>Debit</Type>
<PaymentMethod>Other</PaymentMethod>
- <Tax>
<TaxAmount currency="USD">0.0</TaxAmount>
</Tax>
</Header>
</Invoice>
</DataArea>
</ShowInvoice>

Here the code I'm using :

string xpathExpression = "//Header/Note";


XmlDocument document = new XmlDocument();
document.Load(filename);
XmlNamespaceManager ns = new
XmlNamespaceManager(document.NameTable);
ns.AddNamespace("", "http://www.openapplications.org/
oagis");
ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-
instance");


XmlNode node = document.SelectSingleNode(xpathExpression,
ns);
if (node != null)
{ string note = node.InnerXml.ToString(); }


Everything works( even without using XmlNamespaceManager ) If i
removed all xmlns entries(xmlns="http://www.openapplications.org/
oagis" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openapplications.org/oagis C:
\OAGIS8.0\OAGIS\BODs\ShowInvoice.xsd") from the ShowInvoice root.
But with those entries node is always null. I've tried all the
combination of AddNamespace - nothing worked.
Please help.
Thanks in advance.
 
A

Alberto Poblacion

bbb said:
[...]
XmlNamespaceManager ns = new
XmlNamespaceManager(document.NameTable);
ns.AddNamespace("", "http://www.openapplications.org/
oagis");
ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-
instance");

XmlNode node = document.SelectSingleNode(xpathExpression,
ns);
[...]
Everything works( even without using XmlNamespaceManager ) If i
removed all xmlns entries(xmlns="http://www.openapplications.org/
oagis" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openapplications.org/oagis C:
\OAGIS8.0\OAGIS\BODs\ShowInvoice.xsd") from the ShowInvoice root.
But with those entries node is always null. I've tried all the
combination of AddNamespace - nothing worked.


Check your "xpathExpression" and verify that you are including the
adequate prefixes as you defined them in the AddNamespace instructions.
For instance, here is a search that I do on google maps:

nsmgr.AddNamespace("google",
"http://earth.google.com/kml/2.0");
//...
XmlNode node =
xd.SelectSingleNode("//google:point/google:coordinates",nsmgr);
 
B

bbb

[...]
XmlNamespaceManager ns = new
XmlNamespaceManager(document.NameTable);
ns.AddNamespace("", "http://www.openapplications.org/
oagis");
ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-
instance");
XmlNode node = document.SelectSingleNode(xpathExpression,
ns);
[...]
Everything works( even without using XmlNamespaceManager ) If i
removed all xmlns entries(xmlns="http://www.openapplications.org/
oagis" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openapplications.org/oagisC:
\OAGIS8.0\OAGIS\BODs\ShowInvoice.xsd") from the ShowInvoice root.
But with those entries node is always null. I've tried all the
combination of AddNamespace - nothing worked.

Check your "xpathExpression" and verify that you are including the
adequate prefixes as you defined them in the AddNamespace instructions.
For instance, here is a search that I do on google maps:

nsmgr.AddNamespace("google",
"http://earth.google.com/kml/2.0");
//...
XmlNode node =
xd.SelectSingleNode("//google:point/google:coordinates",nsmgr);

Thanks for reply, but if you probably didn't notice- there is NO ANY
prefixes in the XML itself - just simple tags, so the whole
AddNamespace part of the code can be removed if I removed all xmlns
entries(xmlns="http://www.openapplications.org/oagis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openapplications.org/oagis C:
\OAGIS8.0\OAGIS\BODs\ShowInvoice.xsd") from the ShowInvoice root and
the code will work. But unfortunately the file comes with those
elements ...
 
A

Alberto Poblacion

bbb said:
Thanks for reply, but if you probably didn't notice- there is NO ANY
prefixes in the XML itself - just simple tags,

It doesn't matter. If the XML header declares a default namesace, then
all the tags in the file belong to that namespace even if it is not
indicated with a prefix in the tag. One way to make the XPath queries work
is to add that namespace to the XmlNamespaceManager with an arbitrary
prefix, and then write that prefix in front f the tags in the XPath
expressions.
 
B

bbb

It doesn't matter. If the XML header declares a default namesace, then
all the tags in the file belong to that namespace even if it is not
indicated with a prefix in the tag. One way to make the XPath queries work
is to add that namespace to the XmlNamespaceManager with an arbitrary
prefix, and then write that prefix in front f the tags in the XPath
expressions.

Thanks for reply, but I've completely lost. Can you please tell me how
xpathExpression needs to be in my case.
Thanks in advance.
 
M

Martin Honnen

bbb said:
Thanks for reply, but I've completely lost. Can you please tell me how
xpathExpression needs to be in my case.

You need to bind a prefix to the default namespace URI e.g.

ns.AddNamespace("pf", "http://www.openapplications.org/oagis");

then use that prefix in XPath expressions to qualify element names e.g.
string xpathExpression = "//pf:Header/pf:Note";
so you then have

XmlNode node = document.SelectSingleNode(xpathExpression,
ns);
 
B

bbb

You need to bind a prefix to the default namespace URI e.g.

ns.AddNamespace("pf", "http://www.openapplications.org/oagis");

then use that prefix in XPath expressions to qualify element names e.g.
string xpathExpression = "//pf:Header/pf:Note";
so you then have

XmlNode node = document.SelectSingleNode(xpathExpression,
ns);

Thank you very much - it works now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top