Visual Basic > Visual Basic Forums > Visual Basic Language > Functions and Nested XML Literals
Ask a questionAsk a question
 

AnswerFunctions and Nested XML Literals

  • Friday, November 06, 2009 6:29 PMSimon Woods Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I am constructing some XML using XML literals. I have a function which returns an XElement.

    Function RootElementFormattingFunction() as XElement
        Return <Root Data="foo"/>
    End Function

    In reality it is a bit more complicated than that, but it is called from several places.

    Now I have linq sequence which generates a set of nodes which I want to make children of the XElement returns

         <%= From row in _dataset Select _helper.ConstructChildXElement(row)

    Function ConstructXML() as XElement

        Return <%= RootElementFormattingFunction %>
                   ????

    End Function

    Now I know that I could pass the linq as an argument to RootElementFormattingFunction but because that function is called from several places, the linq sequence may not be applicate in certain scenarios. So, ideally, I want to make all the nodes returned by linq sequence children on the Root node in the ConstructXML function.

    However, I'm not sure of the syntax I need

    For example I tried

            Return <%= RootElementFormattingFunction.Add(linq sequence here ) %>

    but that obviously didn't work.

    So I'd be grateful if you could show me the syntax for what I am trying to achieve.

    Many thanks

    Simon





    • Edited bySimon Woods Friday, November 06, 2009 6:30 PMImprove layout
    •  

Answers

  • Saturday, November 07, 2009 9:11 AMSimon Woods Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Okay so I'm sorted ... pretty simple really ... think I was being too much of an "XML literal universalist". I'm sure you probably could implement this via XML literals.

    Function ConstructXML() as XElement

       Dim parent = RootElementFormattingFunction
       Dim children = From row in _dataset Select _helper.ConstructChildXElement(row)

       parent.add(children)

       return New XElement("MyXML", parent)

    End Function

    Should Add not be a function returning the parent as an XElement? (Yes ... extension methods etc ...)

    Thx

    Simon
    • Marked As Answer bySimon Woods Saturday, November 07, 2009 9:14 AM
    • Edited bySimon Woods Saturday, November 07, 2009 9:17 AMAdded question at end
    •  

All Replies

  • Saturday, November 07, 2009 9:11 AMSimon Woods Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Okay so I'm sorted ... pretty simple really ... think I was being too much of an "XML literal universalist". I'm sure you probably could implement this via XML literals.

    Function ConstructXML() as XElement

       Dim parent = RootElementFormattingFunction
       Dim children = From row in _dataset Select _helper.ConstructChildXElement(row)

       parent.add(children)

       return New XElement("MyXML", parent)

    End Function

    Should Add not be a function returning the parent as an XElement? (Yes ... extension methods etc ...)

    Thx

    Simon
    • Marked As Answer bySimon Woods Saturday, November 07, 2009 9:14 AM
    • Edited bySimon Woods Saturday, November 07, 2009 9:17 AMAdded question at end
    •