vb.net - LINQ to XML when a node does not exist -


I read RSS feeds from various sources and write a common class to consolidate into a collection of objects in VB.net Was there.

Basically the function - Using XML - using LINQ is working properly, but I have a problem when I try to read RSS feeds that do not have any nodes (as You know, many of them are optional). The returned value will not be empty string or nothing, but instead I get a runtime error. I searched the web for the same problem and I found this post, which apparently interprets a workaround, But it does not work with my code.

I am also surprised by the small resources being aware of this issue, so now I am thinking that I am also putting a question in the right questions ...

The following code can be found:

  Moderate PostMen = From the blog post in the XMLSource ("item") order date from date. Perse (blog post. Element ("pubadet") value) New post (descending) With {.Title = BlogPost.Element ("title"). Value, .Link = BlogPost price. ("Link"). value,. Description = blogpost. Element ("description"). Value, .Austric = blogpost. Element ("Author"). value,. Category = (select blog.post.discant ("category") from tag cat = tag.FirstNode.ToString). Catalog list, pubadat = dateTime.parse (blogpost.helent ("pubDate"). Value), .get = BlogPost.Element ( "Guid"). Value}  

I tried to do this and it worked, but the following code will generate exceptions:

  slow postmen = XMLSource.Descendants from the blog post ( "Item") in order to dateTime.Parse (BlogPost. Element ("pubDate"). Value) with descending new post () {.Title = BlogPost.Element ("title"). Values,. Link = BlogPost.Element ("link"). value,. Description = BlogPost element ("Description"). Value, .AuthorText = BlogPost.Element ("Author"). value,. Category = (From the tag in the blog post, select the discendant ("category") cat = tag. FirstNode.ToString). Catalog list,. Pubetate = datetime. Parse (blog post. Element ("pubadet"). Value). Body = blog post Element ("guide"). Values,. Source = CTP (blogpost.alment ("source"). Value, string  

< P>
"post-text" itemprop = "text">

If you try to evaluate value (etc.) - yes, it will break - though, you can try castings ( Apoll, but my example is C # - you have to imagine VB):

  select new {name = (string) el.Element ("abc") ...}  

Clear static conversion operator of empty nodes It accepts and gives proper taps for more complex scenarios, just check it: Select

  child = el.Element ("SomeChild") New {name = child == null ? (String) null: (string) child.Attribute ("name") ...}  

example is hard to be more specific without xml / code ...


Edit your update; The problem is that you are still reading . Value ;

 . Source = CType (BlogPost.Element (" Source "), string  

is a conversion operator from the excelment to < Code> string ; You do not have to look at . Value .


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -