Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 842583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:59:13+00:00 2026-05-15T05:59:13+00:00

I have a source piece of xml into which I want to insert multiple

  • 0

I have a source piece of xml into which I want to insert multiple elements which are created dependant upon certain values found in the original xml

At present I have a sub which does this for me:

<Extension()>
Public Sub AddElements(ByVal xml As XElement, ByVal elementList As IEnumerable(Of XElement))

    For Each e In elementList
        xml.Add(e)
    Next

End Sub

And this is getting invoked in a routine as follows:

Dim myElement = New XElement("NewElements")

myElement.AddElements(
     xml.Descendants("TheElements").
     Where(Function(e) e.Attribute("FilterElement") IsNot Nothing).
     Select(Function(e) New XElement("NewElement", New XAttribute("Text", e.Attribute("FilterElement").Value))))

Is it possible to re-write this using Linq syntax so I don’t need to call out to the Sub AddElements but could do it all in-line

Many Thx

Simon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:59:13+00:00Added an answer on May 15, 2026 at 5:59 am

    Sure:

    Dim outputxml = 
       New XElement("NewElements",
          xml.Descendants("TheElements").
          Where(Function(e) e.Attribute("FilterElement") IsNot Nothing).
          Select(Function(e) _
             New XElement("NewElement", 
                New XAttribute("Text",e.Attribute("FilterElement").Value)
             )
          )
       )
    

    XElement and XAttribute have constructors which (in addition to the element or attribute name) accept an arbitrary number of objects (which themselves can be queries or other IEnumerables). Anything you pass to the constructor is added as content.

    You may also want to look into XML-literals, which make this much more readable but essentially do the same thing.

    With XML Literals, it looks like this:

    dim outputxml = 
       <NewElements><%=   
          From e In xml...<TheElements> 
          Where e.@FilterElement IsNot Nothing 
          Select <NewElement Text=<%= e.@FilterElement %>/>
       %></NewElements>
    ' you can embed names and attribute values too
    
    • <%= %> embeds a VB expression’s value in XML
    • xml...<elemname> selects descendants of xml called elemname
    • ‘elem.@attrname` gets the value of an attribute

    It’s almost XQuery ;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
I am debugging a third-party DLL for which I don't have the source code.
Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?
I have a piece of JavaScript string, coming from an untrusted source, embedded inside
I have a piece of data. At the moment, it's an XML file, but
I have a piece of code for some validation logic, which in generalized for
Suppose I compile a source file file which contains this piece of code, struct
I have a simple piece of script (which uses RegEx) which cleans up a
I have source in a bunch of subdirectories like: src/widgets/apple.cpp src/widgets/knob.cpp src/tests/blend.cpp src/ui/flash.cpp In
I have an executable that defaults to 32-bit. It doesn't have source code and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.