How to handle  "Sequence contains no emements" trying to extract filename from html string

Posted on 2011-04-25
Last Modified: 2012-05-11
I'm trying to grab the filename from an HTML string but if the "src" attribute is currently "src=""", an empty string. I get a "Sequence contains no elements" I try to catch the error like below and I still get the error on my "trap".  I know I'm not doing it right.  Please help.  :-)  Do you think  
= ""
would work, instead of
is Nothing

EDIT:  I tried changing:
If Not root.Descendants.Elements("sigImage").Elements.First().Attributes("src").FirstOrDefault() Is Nothing Then

If Not root.Descendants.Elements("sigImage").Elements.First().Attributes("src").FirstOrDefault().ToString = ""

and that seems to work after a single test.  I'll leave this question open for suggestions because I'm far from confident in my solution overall.  Thanks for any suggestions or help.

EDIT2: Nope.  I tried it on a Windows 7 machine and still got  "Sequence contains no elements".
************** Exception Text **************
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at WorkForceOneSignature.frmWorkForceOneSignature.ReadSignature()
   at WorkForceOneSignature.frmWorkForceOneSignature.frmWorkForceOneSignature_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Open in new window

If Not root.Descendants.Elements("sigImage").Elements.First().Attributes("src").FirstOrDefault() Is Nothing Then
    Dim sigImageName As String = root.Descendants.Elements("sigImage").Elements.First().Attributes("src").FirstOrDefault().Value.ToString      'Get the <img src="filename" /> tag of the custom image from the html file between the <logoimage> tags.
    If sigImageName <> "" Then
        ' Dim imagefilename As String = sigImageName  'Extract the filename from the <img src="filename" /> tag.
        Me.PictureBoxLogo.SizeMode = PictureBoxSizeMode.AutoSize
        Me.PictureBoxLogo.Image = New System.Drawing.Bitmap(InitialPath + "\" + sigImageName)
    End If
End If

Open in new window

Question by:megnin
    LVL 24

    Accepted Solution

    I usually take a two-step approach to this. This first is the LINQ query; the second is checking the count of the result set.

            Dim result As IEnumerable(Of String) = root.Descendants.Elements("sigImage").Elements.First().Attributes("src")
            If results.count > 0 Then
                ' do work here
            End If
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi megnin;

    If you variable root is of type XElement then change your outer if statement to this
    If root.Elements("sigImage").FirstOrDefault().HasElements then

    If you variable root is of type XDocument then change your outer if statement to this
    If root.Descendants("sigImage").FirstOrDefault().HasElements then

    If you are still having issues please post a copy of your XML that causes the issues and one that does not.

    LVL 1

    Author Closing Comment

    Thank you so much!  That worked fine.
    LVL 24

    Expert Comment

    by:Jeff Certain
    Glad I could help

    Featured Post

    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.

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now