Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Uncheck this if you are not a bot PinmemberjasonHall7-Jul-14 10:20 
GeneralRe: Uncheck this if you are not a bot Pinmemberjesarg7-Jul-14 10:49 
GeneralRe: Uncheck this if you are not a bot PinprotectorOriginalGriff8-Jul-14 1:38 
GeneralXPath oddity PinmemberPIEBALDconsult3-Jul-14 8:15 
For something I'm working on, I want to test a given XmlElement with a provided XPath predicate to determine whether or not it's the droid node I'm looking for.
 
public static bool
IsMatch
(
  this System.Xml.XmlElement Element
,
  string                     Predicate
)                                     
{                                     
  return ( Element.SelectSingleNode ( "self::node()[" + Predicate + "]" ) != null ) ;
}
 
I wasted an hour trying to get .[predicate] to work.
 
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
 
doc.LoadXml ( "<Foo Bar='Baz' />" ) ;
 
System.Console.WriteLine ( doc.DocumentElement.SelectSingleNode ( "." ).OuterXml ) ;
System.Console.WriteLine ( doc.DocumentElement.SelectSingleNode ( "self::node()[@Bar='Baz']" ).OuterXml ) ;
System.Console.WriteLine ( doc.DocumentElement.SelectSingleNode ( "self::*[@Bar='Baz']" ).OuterXml ) ;
System.Console.WriteLine ( doc.DocumentElement.SelectSingleNode ( ".[@Bar='Baz']" ).OuterXml ) ;
 
<Foo Bar="Baz" />
<Foo Bar="Baz" />
<Foo Bar="Baz" />
System.Xml.XPath.XPathException: '.[@Bar='Baz']' has an invalid token.
 
The book I have says . is equivalent to self::node(), but that's not true,
. returns a node, whereas self::node() returns a node-set, and you can only apply a predicate to a node-set. Sigh | :sigh:
 

Of course, if you know of a way to test a node that uses neither SelectSingleNode nor SelectNodes, I'd be glad to see it.
You'll never get very far if all you do is follow instructions.

GeneralRe: XPath oddity PinprofessionalRichard Deeming3-Jul-14 8:39 
GeneralRe: XPath oddity PinmemberPIEBALDconsult3-Jul-14 9:21 
GeneralComparisons... Pinmemberjan larsen30-Jun-14 23:30 
GeneralRe: Comparisons... PinprofessionalBernhard Hiller1-Jul-14 20:56 
GeneralRe: Comparisons... Pinmemberjan larsen4-Jul-14 2:46 
GeneralRe: Comparisons... PinprofessionalBernhard Hiller4-Jul-14 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 22 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid