• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2187
  • Last Modified:

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

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

  • 2
1 Solution
Jeff CertainCommented:
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
Fernando SotoCommented:
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.

megninAuthor Commented:
Thank you so much!  That worked fine.
Jeff CertainCommented:
Glad I could help

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now