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

  • SEARCH
  • Home
  • 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 6996515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:10:00+00:00 2026-05-27T20:10:00+00:00

string myTagData = some valid xml data; I want to write the LINQ to

  • 0

string myTagData = “some valid xml data”;

I want to write the LINQ to XML Query that can convert XML1 and XML2 to resultant XML1 and resultant XML2 , if abc tag exists then insert the content of myTagData as last child of abc tag otherwise if abc tag doesn’t exist add myTagData content as last child of Root Element.

XML1

<data>
  <abc>
    <tag1></tag1>
    <tag2></tag2>
  </abc>     
</data>

XML2

<data>

<data>
    <cde>  
    </cde>
    <xyz>
    </xyz>   
</data>

Resultant xml 1

<data>
      <abc>
        <tag1></tag1>
        <tag2></tag2>
        <myTag></myTag>
      </abc>
      <bcd>
      </bcd>         
    </data>

Resultant XML2

<data>
    <cde>  
    </cde>
    <xyz>
    </xyz> 
    <myTag></myTag>   
</data>
  • 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-27T20:10:01+00:00Added an answer on May 27, 2026 at 8:10 pm

    That’s easy. Assuming you’ve got a variable data representing the <data> element, and myTagData is an element you want to add:

    XContainer whereToAdd = data.Element("abc") ?? (XContainer) data;
    whereToAdd.Add(myTagData);
    

    That’s using the null-coalescing operator as an easy way of switching between the two options… because the Element method returns null if the requested element doesn’t exist. If you’re not comfortable with the null-coalescing operator, you may find this simpler to understand:

    XElement abc = data.Element("abc");
    if (abc != null)
    {
        abc.Add(myTagData);
    }
    else
    {
        data.Add(myTagData);
    }
    

    Personally I refer the concise version though 🙂

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

Sidebar

Related Questions

I have written a LINQ to XML query that does what I want, but
string x; foreach(var item in collection) { x += item+,; } can I write
I'll say up front that I am doing some really scary things with linq
I've been given some XML files that don't quite have a proper schema (I
I'm trying to write some code that allows me to switch between SQLCE (locally
I want to insert some data into a table and I want the multiple
I have a short xml I want to get some attribs out of it,
Im trying to write a List of 'Documents' from an XML string, but i
I'm trying to read some information with a REST-Service that uses chunk-encoding. String encodedURL
Greetings, How can I simply encode some binary data into an ASN.1 DER-encoded blob?

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.