Forums

XmlAttributeAttribute

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

XmlAttributeAttribute

Postby terenced » Wed Feb 23, 2011 3:17 pm

I have a class which is Serializable and has a XmlAttributeAttribute [XmlAttribute(AttributeName = "no")]

C# code
Code: Select all
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace TVRage.Model
{
    [Serializable]
    public class Season
    {
        [XmlAttribute(AttributeName = "no")] // This is how to do it in C# :)
        public int Number { get; set; }

        [XmlElement("episode")]
        public List<Episode> Episodes { get; set; }
    }
     
    [Serializable, XmlRoot("episode")]
    public class Episode
    {
        [XmlElement("epnum")]
        public int Number { get; set; }
       
        [XmlElement("seasonnum")]
        public int SeasonNumber { get; set; }
       
        [XmlElement("airdate")]
        public DateTime Airdate { get; set; }
       
        [XmlElement("title")]
        public string Title { get; set; }
    }
}


Cobra code
use System.Xml
use System.Xml.Linq
use System.Xml.Serialization

namespace Broadcatcher.Models

class Season has Serializable

pro number from var as int = 0
has XmlAttribute(AttributeName='no') # the problem is here

pro episodes from var as List<of Episode> = List<of Episode>()
has XmlElement('episode')

class Episode has Serializable, XmlRoot("episode")

pro number from var as int = 0
has XmlElement('epnum')

pro seasonNumber from var as int = 0
has XmlElement('seasonnum')

pro airdate from var as DateTime
has XmlElement('airdate')

pro title from var as String?
has XmlElement('title'))


When I try compiling the above cobra code (on .net and mono), I get the following error:
Code: Select all
 error: Local variables must start with a lowercase letter. This avoids collisions with other identifiers such as classes and enums.


Environments:
Code: Select all
Windows 7 64 bit - .net 4.0 - cobra 0.8.0 post-release
Mac OS X 10.6.6 - Mono JIT compiler version 2.10 (tarball Mon Feb 14 16:04:48 MST 2011) - 0.8.0 post-release


The C# version works on both.

Any ideas? I hope it is just a silly syntax error on my end.
terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: XmlAttributeAttribute

Postby Charles » Wed Feb 23, 2011 6:15 pm

1) Property names in Cobra start with lowercase in Cobra so change line 10:
# from:
has XmlAttribute(AttributeName='no') # the problem is here
# to:
has XmlAttribute(attributeName='no') # the problem is here
# ^

The error message was misleading and needs fixing. I'll look into it.

2)
Next error is the extra right paren on the last line. Easy.

3)
Next error is back at line 10 again. It says that XmlAttribute has no property called "attributeName" which certainly seems true when I look at the docs:
http://msdn.microsoft.com/en-us/library/system.xml.xmlattribute.aspx

Btw those classes don't seem to inherit the Attribute class which seems odd. Also, declaration attributes in both C# and Cobra typically leave out the "Attribute" part of the name. And I think "XmlAttribute" is just using the word "attribute" for things like the "href" in <a href="foo">, not for the .NET declaration kind. It's an overloaded term.

Let me know if you have further questions or problems.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: XmlAttributeAttribute

Postby hopscc » Thu Feb 24, 2011 4:29 am

I suspect the error from 3) is a cobra lookup bug.

It should be (as an attribute lookup) searching for and finding System.Xml.Serialization.XmlAttributeAttribute
(i.e an Attribute subclass or something named <cobraNameAsGiven>Attribute) - instead its hitting on System.Xml.XmlAttribute (first) which is not an Attribute or subclass and then reporting the mismatch.

Possibly a pretty obscure occurrence since there's not that much collision between class names and Attribute (sub) classes.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: XmlAttributeAttribute

Postby hopscc » Thu Feb 24, 2011 4:48 am

Explicitly setting the namespace of the attribute like this
Code: Select all
pro number from var as int = 0
   has System.Xml.Serialization.XmlAttribute(attributeName='no')


unexpectedly gives this confusing diagnostic
Code: Select all
# cobc0 xmlattr.cobra
xmlattr.cobra(10): error: Cannot find a definition for "XmlAttributeAttribute or XmlAttribute" in "Serialization" whose type is "Serialization". There is a member named ".XmlAttributeAttribute" with a similar name.
Compilation failed - 1 error, 0 warnings

which I'd hazard a guess to indicate a lookup against System.Xml.Serialization.XmlAttribute but not a probe for System.Xml.Serialization.XmlAttributeAttribute if the first lookup fails.

However this does compile
Code: Select all
pro number from var as int = 0
   has System.Xml.Serialization.XmlAttributeAttribute(attributeName='no')
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: XmlAttributeAttribute

Postby terenced » Thu Feb 24, 2011 5:50 am

Thanks guys!
@hopscc, it indeed compiles and runs :)
terenced
 
Posts: 15
Location: Montreal, Quebec, Canada

Re: XmlAttributeAttribute

Postby Charles » Thu Feb 24, 2011 10:02 pm

We'll see if we can get that cleaned up soon so that non-attribute classes are skipped in the search. Thanks for sharing this with us.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: XmlAttributeAttribute

Postby hopscc » Fri Feb 25, 2011 2:03 am

(chuck) Make a ticket for it with any pertinent info if you dont want to or wont get to it directly .
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: XmlAttributeAttribute

Postby Charles » Sat Feb 26, 2011 2:50 pm

The problem where "XmlAttribute" is bound to the wrong class is now fixed.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: XmlAttributeAttribute

Postby Charles » Sat Feb 26, 2011 3:25 pm

Random tip inspired by the original posted code:
# You can write this:
pro episodes from var as List<of Episode> = List<of Episode>()
# as this:
pro episodes from var = List<of Episode>()

# You would use the first form if you wanted the property to have a more general type than the initial value:
pro episodes from var as IList<of Episode> = List<of Episode>()

# That's IList vs. List above in case it doesn't stand out.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: XmlAttributeAttribute

Postby Charles » Sat Feb 26, 2011 7:59 pm

I fixed the problem with the poor error message when the attribute property name was capitalized.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 59 guests