Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trap for missing XElement
Message
De
29/11/2008 12:12:54
 
 
À
28/11/2008 11:59:01
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01364420
Message ID:
01364627
Vues:
12
>>Well .Descendants is overloaded - there's a second usage that takes a XName parameter ; ditto with a Ancestors. And Element and Attribute always take parameters so they all need to be methods.....
>
>Not if the properties are Collections (which is what I thought they were) ... then you'd have .Descendants["Customer"] to retrieve one Descendant (which is also a collection).

Not neccessarily only one - there could well me multiple elements named 'Customer'. Also what syntax would you use to get all child elements ? .Descendants[""] would probably come up with nothing so an overloaded method might be the best way.

> But, anyway, if they wanted that sort of functionality, then the method should have been named .GetDescendants, instead of .Descendants. That's all I was saying. You don't agree?

I don't disagree - I guess I'm just ambivalent about it :-}
Best,
Viv



>>>>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 >
>>>
>>>You're welcome Viv. This was the first time I had looked in the System.Xml.Linq namespace. I really did not like the fact that Descendants was a method and not a property! I assumed, given that Descendants is a noun, that it was a property collection. I coudn't find it at first, because I was looking in the docs under XElement's Properties. Then I realized "oh wait ... parentheses, not brackets ... this is a method!" and then I found it in the docs.
>>>
>>>Very poor naming of members in that XElement class, IMHO. Properties should be nouns and Methods should be verbs. There are lots of methods in that class that one would think should be properties: Ancestors, Attributes, Elements, etc. Quite a few of them, actually. Very misleading. These methods should have been named GetAncestors, GetAttributes, etc. if they didn't want them to be properties. Someone wasn't thinking too clearly the day they coded this stuff. <g>
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>
>>>
>>>>
>>>>>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform