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 6836815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:24:05+00:00 2026-05-26T23:24:05+00:00

From the following XML input: <root> <node name=one value=1/> <node name=two value=2/> <node name=three

  • 0

From the following XML input:

<root>
  <node name="one" value="1"/>
  <node name="two" value="2"/>
  <node name="three" value="3"/>
  <node name="four" value="4"/>
</root>";

I need to use LINQ to XML to produce the following:

<root>
  <name content="one"/>
  <value content="1"/>
  <name content="two"/>
  <value content="2"/>
  <name content="three"/>
  <value content="3"/>
  <name content="four"/>
  <value content="4"/>
</root>

This code produces the name elements, but not the value elements.

var input = @"
<root>
  <node name=""one"" value=""1""/>
  <node name=""two"" value=""2""/>
  <node name=""three"" value=""3""/>
  <node name=""four"" value=""4""/>
</root>";


var xml = XElement.Parse(input);
var query = new XElement("root",
    from p in xml.Elements("node")
    select new XElement("name",
        new XAttribute("content", p.Attribute("name").Value) /*,

        new XElement("value", new XAttribute("content", p.Attribute("value").Value)) */
        )
    );

If I include the value XElement (commented out above) inside the last parenthesis then it is a child of the name element, but outside the closing parenthesis it no longer has access to q (it is outside the query).

It feels like I need to concatenate two XElements together or somehow include them in another collection that doesn’t produce any XML.

  • 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-26T23:24:06+00:00Added an answer on May 26, 2026 at 11:24 pm

    You could flatten the attributes using the Enumerable.SelectMany method. In query format this is equivalent to two from clauses:

    var query = new XElement("root",
        from p in xml.Elements("node")
        from a in p.Attributes()
        select new XElement(a.Name,
            new XAttribute("content", a.Value)
            )
        );
    

    To contrast, using the actual SelectMany method and writing it fluently would look like this:

    var query = new XElement("root",
            xml.Elements("node")
               .SelectMany(n => n.Attributes())
               .Select(a => new XElement(a.Name,
                    new XAttribute("content", a.Value))));
    

    However, I tend to find the query syntax to be clearer in most of SelectMany usages and I tend to stick to one format or another, although it’s perfectly fine to mix both.

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

Sidebar

Related Questions

I used DataSet to load a schema from following xml file; <node id=0> <node
How can I get the connection.connection_string value from the following hibernate xml file using
I have the following XML LINQ query from my XDocument. var totals = (from
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for
If I have the following struts.xml config: <action name=input> <result>/jsp/input.jsp</result> </action> <action name=result> <result>/jsp/result.jsp</result>
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I have the following xml that's sent to me from a web service. I'm
Following on from my recent question regarding parsing XML files in Java I have
Given the following repository URL from my pom.xml how can I determine what the

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.