The only thing worse than my XPath skills are my RegEx skills. I don’t use XPath all that much these days, but today I needed to. It took me a while to find a code sample that does this. After a playing a bit with SnippetCompiler, I found the solution:
StringBuilder builder = new StringBuilder();
builder.AppendLine("<foo>");
builder.AppendLine("<bar baz=\"a\">1</bar>");
builder.AppendLine("<bar>2</bar>");
builder.AppendLine("<bar baz=\"b\">3</bar>");
builder.AppendLine("<bar>4</bar>");
builder.AppendLine("<bar baz=\"c\">5</bar>");
builder.AppendLine("</foo>");
XmlDocument doc = new XmlDocument();
doc.LoadXml(builder.ToString());
XmlNodeList nodes = doc.SelectNodes("//foo/bar[not(@baz)]");
if ( nodes == null )
{
WL("nodes was null");
}
else
{
foreach ( XmlNode node in nodes )
{
WL("Node: " + node.InnerText);
}
}
