Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2008

Tip: Looking for answers? Try searching our database.

LINQ and Where clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James - 04 Mar 2008 16:46 GMT
Hello everyone,
I need to know how to use a WHERE clause to weed out elements that are not
present. If I remove the where altogether and all elements are present app
runs as should. However, sometimes the element "ThumbNail" is not present
and app fails - stories not set.

I have tried:
Where story.Element("thumbnail") is not nothing
Where story.Element("thumbnail") <> ""
Where story.Element("thumbnail") not nothing

What is missing?  I have included the sub in question and also an example
xml file. If you look at the xml, one story is missing the ThumbNail
element.

************ Piece of code
   Sub DisplayStories(ByVal xmlContent As String)

       Dim XDocEng As New XDocument
       Dim XDoc As XDocument = XDocEng.Parse(xmlContent)

       Dim stories As IEnumerable = From story In XDoc.Descendants("story")
_
                                    Where story.Element("thumbnail"). = ""
_
                                    Select New
clsDiggStory(story.Attribute("id"), story.Element("title"),
story.Element("description"), story.Attribute("href"),
story.Attribute("link"), story.Element("thumbnail").Attribute("src"))

       lst.ItemsSource = stories

   End Sub

*********** xmlcontent
<stories timestamp="1204647550" min_date="1202055540" total="7579"
offset="0" count="5">
 <story id="5524474"
link="http://grandislandcomputers.com/blog.html#9_inch_eeepc"
submit_date="1204647466" diggs="1" comments="0" status="upcoming"
media="news" href="http://digg.com/gadgets/The_New_9_inch_Asus_EEE_Pc">
   <title>The New 9 inch Asus EEE Pc</title>
   <description>The New 9 inch Asus EEE Pc, promises a lot! Read on for
further review!</description>
   <user name="KidDeath" icon="http://digg.com/img/udl.png"
registered="1194646831" profileviews="269" fullname="Kid Death" />
   <topic name="Gadgets" short_name="gadgets" />
   <container name="Technology" short_name="technology" />
   <thumbnail originalwidth="440" originalheight="330"
contentType="image/jpeg"
src="http://digg.com/gadgets/The_New_9_inch_Asus_EEE_Pc/t.jpg" width="80"
height="80" />
 </story>
 <story id="5524441"
link="http://www.engadget.com/2008/03/04/video-meizu-m8-mini-one/"
submit_date="1204647357" diggs="1" comments="0" status="upcoming"
media="news" href="http://digg.com/gadgets/Video_Meizu_M8_mini_One">
   <title>Video: Meizu M8 mini One</title>
   <description>It doesn't even work. You can't even call this as a
prototype.</description>
   <user name="muzocan" icon="http://digg.com/users/muzocan/l.png"
registered="1204026081" profileviews="12" fullname="Muzaffer Can" />
   <topic name="Gadgets" short_name="gadgets" />
   <container name="Technology" short_name="technology" />
   <thumbnail originalwidth="440" originalheight="330"
contentType="image/jpeg"
src="http://digg.com/gadgets/Video_Meizu_M8_mini_One/t.jpg" width="80"
height="80" />
 </story>
 <story id="5524290"
link="http://www.javoedgeblog.com/2008/03/04/t303-is-sony-ericssons-latest-petite-slider/"
submit_date="1204646821" diggs="1" comments="0" status="upcoming"
media="news"
href="http://digg.com/gadgets/T303_Is_Sony_Ericsson_s_Latest_Petite_Slider">
   <title>T303 Is Sony Ericssonâ?Ts Latest Petite Slider</title>
   <description>The new Sony Ericsson T303 may not be the Xperia X1, but it
looks like a tiny, simple slider phone that will fit in any pocket. Sony
Ericsson says that it combines â?opremium finish and materials with a good
feature-set to deliver style with substance,� which is probably their own
stylish but insubstantial way of saying..</description>
   <user name="JAVOedge" icon="http://digg.com/users/JAVOedge/l.png"
registered="1175193503" profileviews="1218" />
   <topic name="Gadgets" short_name="gadgets" />
   <container name="Technology" short_name="technology" />
   <thumbnail originalwidth="463" originalheight="286"
contentType="image/jpeg"
src="http://digg.com/gadgets/T303_Is_Sony_Ericsson_s_Latest_Petite_Slider/t.jpg"
width="80" height="80" />
 </story>
 <story id="5524228"
link="http://www.businessweek.com/technology/content/feb2008/tc20080229_871649.htm"
submit_date="1204646609" diggs="2" comments="0" status="upcoming"
media="news"
href="http://digg.com/gadgets/Widgets_The_Future_of_Online_Ads">
   <title>Widgets: The Future of Online Ads</title>
   <description>Look to the evolution of television advertising to
understand the necessity of widgets in today's online world</description>
   <user name="paisley" icon="http://digg.com/users/paisley/l.png"
registered="1132241682" profileviews="3481" fullname="dj paisley amoeba" />
   <topic name="Gadgets" short_name="gadgets" />
   <container name="Technology" short_name="technology" />
 </story>
 <story id="5524209"
link="http://www.aboutprojectors.com/news/2008/03/04/infocus-play-big-in83-projector-c
oming-soon/
"
submit_date="1204646524" diggs="1" comments="0" status="upcoming"
media="news"
href="http://digg.com/gadgets/InFocus_Play_Big_IN83_Projector_Coming_Soon">
   <title>InFocus Play Big IN83 Projector Coming Soon</title>
   <description>Coming later this month is the InFocus Play Big IN83. This
projector features 1600 ANSI lumens and full HD (1920 x 1080)
resolution.</description>
   <user name="AboutProjectors"
icon="http://digg.com/users/AboutProjectors/l.png" registered="1158948006"
profileviews="726" />
   <topic name="Gadgets" short_name="gadgets" />
   <container name="Technology" short_name="technology" />
   <thumbnail originalwidth="300" originalheight="198"
contentType="image/jpeg"
src="http://digg.com/gadgets/InFocus_Play_Big_IN83_Projector_Coming_Soon/t.jpg"
width="80" height="80" />
 </story>
</stories>
Martin Honnen - 04 Mar 2008 17:06 GMT
>        Dim stories As IEnumerable = From story In
> XDoc.Descendants("story") _
>                                     Where story.Element("thumbnail"). =
> "" _

Try
        Where story.Element("thumbnail") IsNot Nothing

Here is a short but complete example that works for me:

        Dim doc As XDocument = <?xml version="1.0"?>
                               <root>
                                   <foo id="f1">
                                       <bar/>
                                   </foo>
                                   <foo id="f2"></foo>
                               </root>
        Dim query = From foo In doc.<root>.<foo> _
                    Where foo.Element("bar") IsNot Nothing _
                    Select New With {.Id = foo.@id}
        For Each item In query
            Console.WriteLine(item.Id)
        Next

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

James - 04 Mar 2008 17:26 GMT
Excellent Martin!!!
I have never used "isnot nothing" but have used "is not nothing"! Thank you
for this, very useful op!

-JD

>>        Dim stories As IEnumerable = From story In
>> XDoc.Descendants("story") _
[quoted text clipped - 19 lines]
>             Console.WriteLine(item.Id)
>         Next

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.