counter in markup in vb xml literal

78 views Asked by At

I have an array of strings like so:
[0] aaa
[1] bbb
[2] ccc

Now I need to create XML like so:

<parent>
    <child_1>aaa</child_1>
    <child_2>bbb</child_2>
    <child_3>ccc</child_3>
</parent>

Now I know in VB I can do:

Dim myXML As XElement = <parent>
                           <%= For c In arrayOfChildren
                               Select <child><%= c %></child>
                        </parent>

The problem is - how to get that nasty counter in?
To clarify: I do not have a choice over programming language, gotta be VB. I also do not have a choice over the structure of XML, it is part of specification we got from an external company.

Any help appreciated, thanks:)

1

There are 1 answers

1
Gideon Engelberth On BEST ANSWER

Although a lesser used feature, you can specify the name of an element as an embedded expression, rather than a literal. Also, you will need to change to the method syntax to get the overload of Select that gives you the item index along with the item.

Dim myXML = <parent>
                <%= children.Select(
                    Function(c, i)
                        Return <<%= "child_" & (i + 1).ToString() %>>
                                    <%= c %>
                                </>
                    End Function) %>
            </parent>