Solved

XPathExpression to select nodes with values

Posted on 2008-10-20
7
311 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
DateTimepicker 4 33
Interview questions for support of a legacy ASP.NET site 4 40
Close form "before" open 3 30
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

22 Experts available now in Live!

Get 1:1 Help Now