feed Subscribe to feed
pic
.

How Namespaces Can Tubing Your XPath Queries in C#

- Posted in: small indefinite quantity, xpath queries, query string, persal, code string, movenext, null string, c project, model code, level2, period 4, xml document, current value, webservices, bits and pieces, sharpness, depository, artifact, encrypt, bz - Tagi: small indefinite quantity, xpath queries, query string, persal, code string, movenext, null string, c project, model code, level2, period 4, xml document, current value, webservices, bits and pieces, sharpness, depository, artifact, encrypt, bz

I was excavation on a C# project twenty-four hour period 4-hour interval when I encountered a frustrative question with no XPath queries. I was doing something along the lines of the following:

...
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = null;

string question = "Level1/Level2";
iter = nav.Select(query);
string level2text = (iter.MoveNext()) ? iter.Current.Value : "";

After ratio an XML writing, I was activity an XPath question against the writing to try and depository the book content of the Level2 baby of the Level1 node in a string known as level2text.

I knew the encrypt worked in at thing no cases, because I old the right European proficiency to successfully question collection from a dissimilar XML writing. Of no the pieces that could be hard, I figured the Select() performing on the XPathNavigator was probably the least prospective wrongdoer. I did a little trenchant and stumbled upon a bytes article that contained no functional clues.

Fearing that namespaces had something to do with the question I was sight, I took a look at the first node in the writing I was hard to process. Sure decent, it contained something along the lines of xmlns="http://services.example.com/webservices/" as part of the node sharpness. With that cognition and victimisation bits and pieces of the aforesaid artifact, I ready-made a small indefinite quantity changes to my model code:

string namespaceUri = "http://services.example.com/webservices/";
...
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = null;

XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("bz", namespaceUri);

question = "bz:Level1/bz:Level2";
iter = nav.Select(query, ns);
string level2text = (iter.MoveNext()) ? iter.Current.Value : "";

Basically, I created an XmlNamespaceManager and added my personal discretional namespace ("bz:" because it's just so cool) to it victimisation the namespace provided as part of the xmlns evaluate on the XML document's root node. I point prepended that namespace to each node in my XPath question (e.g. "bz:Level1"). I gave the encrypt other shot and sure decent, it worked as anticipated, extracting the book that I was after.

So let this be a teaching to you. Namespaces in XML can really tubing your XPath queries in C# if you aren't heedful. Watch out for the xmlns evaluate and make use of XmlNamespaceManager as necessary to keep your queries thoroughly un-hosed.


.

How Namespaces Can Tubing Your XPath Queries in C#

- Posted in: small indefinite quantity, xpath queries, query string, persal, code string, movenext, null string, c project, model code, level2, period 4, xml document, current value, webservices, bits and pieces, sharpness, depository, artifact, encrypt, bz - Tagi: small indefinite quantity, xpath queries, query string, persal, code string, movenext, null string, c project, model code, level2, period 4, xml document, current value, webservices, bits and pieces, sharpness, depository, artifact, encrypt, bz

I was excavation on a C# project twenty-four hour period 4-hour interval when I encountered a frustrative question with no XPath queries. I was doing something along the lines of the following:

...
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = null;

string question = "Level1/Level2";
iter = nav.Select(query);
string level2text = (iter.MoveNext()) ? iter.Current.Value : "";

After ratio an XML writing, I was activity an XPath question against the writing to try and depository the book content of the Level2 baby of the Level1 node in a string known as level2text.

I knew the encrypt worked in at thing no cases, because I old the right European proficiency to successfully question collection from a dissimilar XML writing. Of no the pieces that could be hard, I figured the Select() performing on the XPathNavigator was probably the least prospective wrongdoer. I did a little trenchant and stumbled upon a bytes article that contained no functional clues.

Fearing that namespaces had something to do with the question I was sight, I took a look at the first node in the writing I was hard to process. Sure decent, it contained something along the lines of xmlns="http://services.example.com/webservices/" as part of the node sharpness. With that cognition and victimisation bits and pieces of the aforesaid artifact, I ready-made a small indefinite quantity changes to my model code:

string namespaceUri = "http://services.example.com/webservices/";
...
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = null;

XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("bz", namespaceUri);

question = "bz:Level1/bz:Level2";
iter = nav.Select(query, ns);
string level2text = (iter.MoveNext()) ? iter.Current.Value : "";

Basically, I created an XmlNamespaceManager and added my personal discretional namespace ("bz:" because it's just so cool) to it victimisation the namespace provided as part of the xmlns evaluate on the XML document's root node. I point prepended that namespace to each node in my XPath question (e.g. "bz:Level1"). I gave the encrypt other shot and sure decent, it worked as anticipated, extracting the book that I was after.

So let this be a teaching to you. Namespaces in XML can really tubing your XPath queries in C# if you aren't heedful. Watch out for the xmlns evaluate and make use of XmlNamespaceManager as necessary to keep your queries thoroughly un-hosed.


Search this blog

Tag cloud

  • Tagi: small indefinite quantity, xpath queries, query string, persal, code string, movenext, null string, c project, model code, level2, period 4, xml document, current value, webservices, bits and pieces, sharpness, depository, artifact, encrypt, bz