Coffeehouse Thread

21 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Are XML string literals possible in C#?

Back to Forum: Coffeehouse
  • User profile image
    vesuvius

    Just found a really really useful feature on the VB site that alllows one to easily create excel docs from SQL via linq. Looking at the following diagram



    I very much doubt you can linq to excel via sql unless you use VB. If not I'll have to use VB.

    PS some VB language intricacies (press 'space' instead of 'return ' when coding. Place a '_' at the end of a linq query line - I'd rather not have to go through this strangeness)

  • User profile image
    Ion Todirel

    i hope not, in VB while it looks like nice, its crazy, i don't like it, yes of course its better that in C# but there must be a better way... you can't just put a language in other language like that

  • User profile image
    evildictait​or

    No. This is a VB-only feature (and is only available in VB in and after VS2008 also).

  • User profile image
    vesuvius

    Sorry, I wasn't clear, does anyone know a workaround to use the above via C#

  • User profile image
    Sven Groot

    vesuvius wrote:
    Sorry, I wasn't clear, does anyone know a workaround to use the above via C#

    You need to use XDocument and the related classes. This post on my blog gives a short XML literals example and the C# equivalent at the bottom.

  • User profile image
    stevo_

    Interesting that the if ternary is stated as a VB feature, I know this is a break down of the new features, but it doesn't really highlight that C# has been slapping VBs face around for years with its own..

  • User profile image
    Sven Groot

    stevo_ wrote:
    Interesting that the if ternary is stated as a VB feature, I know this is a break down of the new features, but it doesn't really highlight that C# has been slapping VBs face around for years with its own..

    Yeah, I noticed that too. Same is true for Nullable syntax, C# had that in 2005 already.

  • User profile image
    vesuvius

    Sven Groot wrote:
    
    vesuvius wrote:
    Sorry, I wasn't clear, does anyone know a workaround to use the above via C#

    You need to use XDocument and the related classes. This post on my blog gives a short XML literals example and the C# equivalent at the bottom.


    Many thanks Sven.

  • User profile image
    vesuvius

    Ion Todirel wrote:
    i hope not, in VB while it looks like nice, its crazy, i don't like it, yes of course its better that in C# but there must be a better way... you can't just put a language in other language like that


    Now i think of it, I wholly agree with you. I literally (is this the derivation of the 'XML literal' nomenclarure?) did a copy and paste of XML into the VB compiler. It did not seem so strange becuase I was still getting used to the fact that pressing enter/return after intellisense in VB ignores the intellisense and you need to press space. Also the use of the underscore is very different (I guess this is complaining that German is not like English). XDocument seems a more formal approach.

  • User profile image
    littleguru

    Btw., just as a side note, the VB.NET XML literals are translated to XDocument, XElement calls when the VB.NET compiler compiles them.

  • User profile image
    vesuvius

    littleguru wrote:
    Btw., just as a side note, the VB.NET XML literals are translated to XDocument, XElement calls when the VB.NET compiler compiles them.


    So XML literals are no more than 'syntactic sugar'. VB allows users to paste XML, that is then accessed the same way C# does using the same classes anyway?

  • User profile image
    littleguru

    vesuvius wrote:
    
    littleguru wrote:
    Btw., just as a side note, the VB.NET XML literals are translated to XDocument, XElement calls when the VB.NET compiler compiles them.


    So XML literals are no more than 'syntactic sugar'. VB allows users to paste XML, that is then accessed the same way C# does using the same classes anyway?


    sugar, compiler magic, call it as you want Smiley
     
    Compile a method that uses xml literals and open it in reflector. you'll see that they only got converted to XElement, XDocument caslls.

  • User profile image
    Sven Groot

    vesuvius wrote:
    It did not seem so strange becuase I was still getting used to the fact that pressing enter/return after intellisense in VB ignores the intellisense and you need to press space.

    Tab also works, as does any character that would come after the identifier (like '(' or a dot or an operator).

    vesuvius wrote:
    is this the derivation of the 'XML literal' nomenclarure

    No. If you have something like "Hello world" in your code, it's called a string literal. XML literals are similar, except that they define XML instead of plain text strings.

  • User profile image
    wkempf

    Ion Todirel wrote:
    i hope not, in VB while it looks like nice, its crazy, i don't like it, yes of course its better that in C# but there must be a better way... you can't just put a language in other language like that


    I always find these types of comments silly (please don't take offense, as none was meant).  Many languages allow you to create DSLs and use them directly within your code.  People using those languages find this to be a good thing! You can already create DSLs in C#, you're just highly constrained with the syntax you can use.  The XDocument examples given as the C# equivalent ARE such a DSL.  Very close to "XML literals", but with some "pesky" syntax differences.  LINQ also has a DSL with "pesky" syntax, but C# included the sugar to allow you to write the queries in a form that loses these "pesky" syntactic differences.  Do you have a problem with LINQ embedding a different language within C#?  Why shouldn't C# also provide the syntactic sugar to allow XML literals?  Better yet, why shouldn't C# allow us to create our own DSLs with greater syntactic freedom?

  • User profile image
    Ion Todirel

    wkempf wrote:
    
    Ion Todirel wrote:
    i hope not, in VB while it looks like nice, its crazy, i don't like it, yes of course its better that in C# but there must be a better way... you can't just put a language in other language like that


    I always find these types of comments silly (please don't take offense, as none was meant).  Many languages allow you to create DSLs and use them directly within your code.
    i'm not a fan of object-oriented rendering either, but there must be a better way, just look at LINQ, the problem is not with xml alone, what thy did is fine but it works only for xml, i don't know i just don't like it

  • User profile image
    weitzhandler

    Ion Todirel said:
    i hope not, in VB while it looks like nice, its crazy, i don't like it, yes of course its better that in C# but there must be a better way... you can't just put a language in other language like that

    Crazy????

     

    I love it!!!

     

    That's the nicest thing that ever happened to me!

  • User profile image
    W3bbo

    Hmm, thread necromancy.

     

    Unless I'm mistaken, I think that graph in the OP is wrong, C# had has the ternary operator since the beginning and nullable syntax since 2.0.

  • User profile image
    Cannot​Resolve​Symbol

    W3bbo said:

    Hmm, thread necromancy.

     

    Unless I'm mistaken, I think that graph in the OP is wrong, C# had has the ternary operator since the beginning and nullable syntax since 2.0.

    I think it's listing features that were new to the languages at the time.

     

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.