Solved

XPathExpression to select nodes with values

Posted on 2008-10-20
7
316 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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