CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
Explore    Item Entry    Members   
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML





Zone: > NEWSGROUP > Asp.Net Forum > visual_studio.visual_studio_2008 Tags:
Item Type: NewsGroup Date Entered: 1/18/2008 4:26:04 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 2 Views: 5 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
3 Items, 1 Pages 1 |< << Go >> >|
Stan92
Asp.Net User
Linq to Xml attribute1/18/2008 4:26:04 PM

0/0

Hi,

I m trying to parse a rss feed using linq to XML.
I have a little problem with the attribute part.

My feed has a node called <media:content url="blablaba"> and it's a child of the item node.
However, sometimes the item node doesn't have this <media:content> node

From my code, here's what I do.

Xnamespace media=http://search.yahoo.com/mrss/;
XDocument rss=Xdocument.Load(myurl);
var myItems=from item in rss.Descendants("item")
    select new
    {
        title=item.Element("title").Value,
        urlmedia=item.Element(media+"content").Attribute("url").Value
    }

But I get an error (NullReferenceException).

Any idea to fix this problem ?

Stan

rjcox
Asp.Net User
Re: Linq to Xml attribute1/18/2008 8:24:36 PM

0/0

 Identify what you want to set to urlmedia to when the attribute is not present, for this I'll cal it defaultUrl. 

Xnamespace media=http://search.yahoo.com/mrss/;
XDocument rss=Xdocument.Load(myurl);
var myItems=from item in rss.Descendants("item")
    let um = item.Element(media+"content").Attribute("url")
    select new
    {
        title=item.Element("title").Value,
        urlmedia= (um != null) ? um.Value : defaultUrl
    }
 I.e. check for null before getting the value (and the let clause is useful here to avoid repeating the item.Element... sub-expression.

Richard
Stan92
Asp.Net User
Re: Linq to Xml attribute1/18/2008 10:39:28 PM

0/0

Yes.. I thank you.. :)

Stan

3 Items, 1 Pages 1 |< << Go >> >|



Search This Site:


Meet Our Sponsors:



Other Resources:

'LogCreateDate' is a duplicate attribute name' exception - DNN2.12 - ng ... 'LogCreateDate' is a duplicate attribute name' exception - DNN2.12, > ROOT > ... Linq. Other. Regular Expressions. VB.Net. XML. Zone: ...
.NET Language-Integrated Query for XML Data In LINQ to XML, you can treat elements and attributes that contain values in a ... of the object is null, the attribute will be deleted. Getting XML attributes ...
LINQ to XML for XPath Users How to: Filter on an Attribute (XPath-LINQ to XML) ... How to: Find Attributes of Siblings with a Specific Name (XPath-LINQ to XML) ...
LINQ to XML - 5 Minute Overview - Hooked on LINQ Generating an XML document by hand by cascading attributes in the constructor. ... Notice the casting and access methods (.Attribute and .Element) ...
WorldofASP.NET: Using LINQ to XML (and how to build a custom RSS Feed ... In this article, Scott examines how to work with LINQ using XML. ... have a "status" attribute, or whose "status" attribute value is not set to "disabled" ...
[LINQ Newbie] XML Elements have the same name, but different attribute ... [LINQ Newbie] XML Elements have the same name, but different attribute values ... XML nodes before (maybe twice), and I'm totally new to using Linq to XML, and ...
Remove XML with LINQ to XML in ASP.NET In this tutorial we look at how to remove elements and attributes in LINQ to XML. ... Remove attribute. 4. Remove XML in a namespace ...
Language Integrated Query - Wikipedia, the free encyclopedia ... attributes such as primary keys are specified using LINQ to ... ".NET Language-Integrated Query for XML Data". Retrieved on 2007-11-30. ^ "LINQ to SQL" ...
Update XML with LINQ to XML in ASP.NET In this tutorial we look at how to update elements and attributes in LINQ to XML. ... Update attribute. 4. Update XML in a namespace ...
kirupa.com - Further Exploration with LINQ: Page 3 Learn how to use LINQ to make accessing SQL and XML data easier, and also learn ... XML data is also very simple, we just have to pick the element/attribute we ...


 
All Times Are GMT