Solved

XPathExpression to select nodes with values

Posted on 2008-10-20
7
297 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
Comment Utility
Try:

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

Author Comment

by:t-min
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:t-min
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your help
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Update Status Bar.... 1 22
Code enhancement 5 12
Problem to open Excel file 15 38
Why is "packages element is not declared"? 2 37
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now