Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trap for missing XElement
Message
From
28/11/2008 02:37:13
 
 
To
27/11/2008 18:14:55
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01364420
Message ID:
01364451
Views:
13
Thanks Bonnie, that will work fine. I hadn't looked at the problem since posting the message at knocking off time yesterday - I guess that's the lazy man's way of 'sleeping on it' < g >

>Did you figure it out yet? I just started playing with your example, plugging stuff into the Immediate Window and found a solution for you. Check the Count to see if it's 0 or not in your get:
>
>
>public string Customer
>{
>    get 
>    {
>        if (((IEnumerable<XElement>)content.Descendants("Customer")).Count<XElement>() == 0)
>            return "";
>        else
>            return content.Descendants("Customer").Single().Value; 
>    }
>    set { content.Descendants("Customer").Single().Value = value; }
>}
>
>
>~~Bonnie
>
>
>
>
>
>>Hi,
>>
>>I'm using classes that encapsulate an XElement via a bunch of properties. Here's a simple,fictitious example:
>>    public class Order
>>    {
>>        private XElement content = new XElement("Order",
>>            new XElement("ID"),
>>            new XElement("Customer")
>>            );
>>
>>        public XElement XMLContent
>>        {
>>            get { return content; }
>>            set { content = XMLContent; }
>>        }
>>
>>        public string ID
>>        {
>>            get { return content.Descendants("ID").Single().Value; }
>>            set { content.Descendants("ID").Single().Value = value; }
>>        }
>>
>>        public string Customer
>>        {
>>            get { return content.Descendants("Customer").Single().Value; }
>>            set { content.Descendants("Customer").Single().Value = value; }
>>        }
>>    }
>>This works fine until I set the XMLContent to (again a fictitious example - but assume that it is a valid thing to do) an XElement that does not contain a 'Customer' element. In this situation I'd like the Customer property to return an empty string. If I inspect the Customer property in the debugger it shows an exception - but I can't seem to trap for this situation anywhere (I was thinking I could do this in the Customer accessor....)
>>
>>Any suggestions,
>>TIA,
>>Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform