Solved

XPathExpression to select nodes with values

Posted on 2008-10-20
7
324 Views
Last Modified: 2012-05-05
I need an XPathExpression to select nodes containing values. So in the sample XML below, the XPathExpression should return the nodes "fred" and "george", but should not return the node "foo" .

I was thinking it would look something like
XPathExpression sitemapExp = XPathExpression.Compile("//MyXML/MyItems/[exists(myValue)]");

Any ideas appreciated
<MyXML xmlns="">
  <MyItems>
    <item id="foo"><![CDATA[Foo]]></item>
    <item id="fred" myValue="aValue"><![CDATA[fred]]></item>
    <item id="george" myValue="aValue"><![CDATA[george]]></item>
  </sections>
</MyXML>

Open in new window

0
Comment
Question by:t-min
  • 4
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 300 total points
ID: 22763588
Try:

XmlNodeList lst = doc.SelectNodes(@"//item[@myValue]");
0
 

Author Comment

by:t-min
ID: 22763732
Thanks. Tested it an only the myValue nodes. Like this
myValue="left"myValue="left"myValue="left"myValue="top"myValue="bottom"myValue="bottom"myValue="top"myValue="left"myValue="left"

All these nodes have values which is part of my requirements, but it is the item nodes that I want returned.

0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 200 total points
ID: 22764356
i dont quite really understand the problem but i am assuming that you want to reterive the myValue attributes value from the xml

if thats the case then this will work or else please explain your problem a little bit more so that we can provide a better solution
XmlNodeList list = doc.SelectNodes (@"//item[@myValue]");
for (int i = 0; i < list.Count; i++)
{
      Console.WriteLine(list[i].OuterXml);
      Console.WriteLine (list[i].Attributes.GetNamedItem("myValue").Value);
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:t-min
ID: 22764736
No I don't want to select the myValue nodes.

What I need is an XPathExpression as I am using it with XSL. The item nodes are needed by the XSL. I need an expression that will select all item nodes where myValue is not null.

In my question I stated that I wanted to return the nodes "fred" and "george" from my sample XML. It might have been clearer if I had said the item nodes with the ids "fred" and "george". I am interested in the item nodes that have something for myValue.

Does this help explain it more?
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 300 total points
ID: 22771130
Are you sure your testing is right? Ragi0017 shows my xpath is OK.

Try yourself my code,
and the output is as below:

<item id="fred" myValue="left"><![CDATA[fred]]></item>
<item id="george" myValue="left"><![CDATA[george]]></item>

By the way, the xml file is:
<MyXML xmlns="">
      <MyItems>
            <sections>
                  <item id="foo"><![CDATA[Foo]]></item>
                  <item id="fred" myValue="left"><![CDATA[fred]]></item>
                  <item id="george" myValue="left"><![CDATA[george]]></item>
            </sections>
      </MyItems>
</MyXML>

It would be nice if next time you provide working xml files so we do not need to fix them. You version has several errors.


            XmlDocument doc = new XmlDocument();
            doc.Load("XMLFile1.xml");
            XmlNodeList lst = doc.SelectNodes(@"//item[@myValue]");
            string s = "";
            foreach (XmlNode n in lst)
            {
                //s += ((XmlElement)n).Attributes.GetNamedItem("id").Value + " | ";
                s += ((XmlElement)n).OuterXml + Environment.NewLine;
            }
            MessageBox.Show(s);

Open in new window

0
 

Author Comment

by:t-min
ID: 22772687
Thanks for your ideas. I have found a solution based on the above suggetion..

XPathExpression sitemapExp = XPathExpression.Compile("//MyItems/item[@myValue]");

0
 

Author Closing Comment

by:t-min
ID: 31508101
Thanks for your help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
Database Connections Not being returned to Connection Pool 7 33
tableview is not updating 1 20
location of a form 2 24
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question