We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

megnin
megnin asked
on
Medium Priority
2,526 Views
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

to:
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

Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.

Fernando

Author

Commented:
Thank you so much!  That worked fine.
Glad I could help
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.