?
Solved

XPathExpression to select nodes with values

Posted on 2008-10-20
7
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1200 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 800 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 

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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1200 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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:…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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