2

I write a lot of code using System.Xml.Linq in VB.net and c#, but there is a few things I can do in VB that I haven't worked out how to do in c#. Could anyone tell me how to do the following examples

Public Sub Test()

       'Exmaple 1 - get an attribute value with the .@ Access Method instead of 
       'testVal.Attribute("Test").Value
        Dim test As New XElement("Test", New XAttribute("Test", "My Value"))
        Dim testVal = test.@Test
        'testVal equals  "My Value"

        'Exmaple 2 - create an element by just typing xml
        Dim test2 As XElement = <Test Test="My Value"></Test>

        'Exmaple 3 - not much of a priority - but using the following syntax to get elements 
        'rather than test.Decendents("Cars").Elements("Car")
        Dim x = test...<Cars>.<Car>
        'Returns my IEnumerable<XElement>

End Sub

I know how to do the above examples using various methods etc. But was wondering if there is a way of writing code in the way I have in VB in C#. The sort of shorthand way I suppose. Can I reference visual basic assemblies to achieve this for example?

Thanks in advance! :)

Jack
  • 21
  • 1
  • 2
    No, you can't. C# deliberately doesn't have any XML-specific syntax. Personally I'm glad of that, but regardless of the merits, it was a definite decision which I wouldn't expect to change. – Jon Skeet Feb 19 '21 at 12:28
  • 3
    [Never! We won't bake in a specific format.](https://github.com/dotnet/roslyn/issues/3912) - Mads Torgersen – Crowcoder Feb 19 '21 at 12:34
  • @JonSkeet definite decision and the [github issue](https://github.com/dotnet/roslyn/issues/1746) was tagged as "Over Our Dead Bodies". – Cleptus Feb 19 '21 at 13:09
  • Thanks for your comments and links guys! – Jack Feb 21 '21 at 12:14

0 Answers0