Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: XmlElement conversion not implemented for non-literals #253

Closed
Rubicen opened this issue Mar 10, 2019 · 7 comments · Fixed by #815
Closed

VB -> C#: XmlElement conversion not implemented for non-literals #253

Rubicen opened this issue Mar 10, 2019 · 7 comments · Fixed by #815
Labels
enhancement help wanted VB -> C# Specific to VB -> C# conversion

Comments

@Rubicen
Copy link

Rubicen commented Mar 10, 2019

Input code

Dim element As XElement = <SetAttribute XPath=<%= entry.Code %>></SetAttribute>

Erroneous output

XElement element = XElement.Parse("<SetAttribute XPath=<%= entry.Code %>></SetAttribute>");

Expected output

XElement element = new XElement("SetAttribute", new XAttribute("XPath", entry.Code));

Details

Product in use: e.g. web converter

Version in use: newest

Any other relevant information to the issue, or your interest in contributing a fix.

@BobFrankston
Copy link

Handling embedded XML is one of the reasons for using VB over C# so converting such code makes it easier to move on.

@GrahamTheCoder
Copy link
Member

Thanks both for raising this, and especially for the example with expected output. My apologies for not commenting sooner.

Looking at the docs this could easily be a big task to implement, but hopefully it'll be possible in stages, starting with simple literal support then later handling various inline replacements, and the query/modify/insert syntax.

Ideally every possible operation doesn't need to be a hardcoded syntax transformation - there will hopefully be a way to generalize our approach, by making use of the semantic model and other parts of the API. The first step of that is to put a few examples into the converter and see what level of information is available from the semantic model.

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented May 2, 2019

At a quick look, there are 21 syntax constructs to convert. If we can't find a consistent way to convert them (especially one that keeps roughly in line with the currently parsed structure), then that will be rather tedious to implement fully.

An example of part of this that (at least on the surface) looks consistent and easy because the structure is the same, and thus the existing per-node visiting conversion works very naturally:

For Each book In From element In catalog.<Catalog>.<Book>
  book.<Price>.Value = book.<Price>.Value * 1.05
Next
foreach (var book in catalog.Elements("Catalog").Elements("Book")) 
{
   book.Element("Price").Value = Conversions.ToDecimal(book.Element("Price").Value) * (decimal) 1.05;
}

@BobFrankston
Copy link

Also, if I understand the rules correctly

   dim a = el.@atr

becomes

  var a = el?.attribute("atr")?.value

@GrahamTheCoder GrahamTheCoder changed the title VB -> C#: Conversion for XmlElement not implemented VB -> C#: XmlElement conversion not implemented for non-literals Jul 21, 2019
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Mar 7, 2020

Description edit: I've updated the erroneous output for the current release.

I was going to add an alternative expected output:

XElement element = XElement.Parse($"<SetAttribute XPath="{entry.Code}"></SetAttribute>");

But I think in the general case the <%= syntax can inject one or many entire elements

@GrahamTheCoder GrahamTheCoder added the VB -> C# Specific to VB -> C# conversion label Jun 30, 2020
@Noah1989
Copy link
Contributor

I am starting on this issue now.

@BobFrankston
Copy link

as an FYI {} by itself was not enough -- you need to do the proper escape codes. String interpolation in C# has a feature similar to tagged strings in JavaScript -- you can say htmlescape<p>${foo}</p> so that if foo is "a>b" it becomes "a>b". I don't remember it offhand but you can do something similar in C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted VB -> C# Specific to VB -> C# conversion
Projects
None yet
4 participants