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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:27:23+00:00 2026-05-13T01:27:23+00:00

I have an xml in the following format <root> <page> <title>text</title> <text attrib1=1>some text</text>

  • 0

I have an xml in the following format

   "<root>"
   "<page>"
   "<title>text</title>"
   "<text attrib1="1">some text</text>"
   "<text attrib1="2">some text</text>"
   "<text attrib1="3">some text</text>"
   "<text attrib1="4">some text</text>"

   "</page>"

   "<page>"
   "<title>text</title>"
   "<text attrib1="1">some text</text>"
   "<text attrib1="2">some text</text>"
   "<text attrib1="3">some text</text>"
   "<text attrib1="4">some text</text>"

   "</page>"
  "</root>"

ignore ” “

now i want the resultant xml like this

      "<root>"
     "<title>text</title>"
     "<text attrib1="4">some text</text>"
     "<title>text</title>"
     "<text attrib1="4">some text</text>"
     "</root>"

can this be achieved in one query?
I tried the following by using two queries

        var titleElms =
            from t in allElements
            select
                new
                {
                    Title = t.Element("title")
                };

        var textElms =
            from t in allText
            where (string)t.Attribute("attrib1").Value == "4"
            select
            t;

I am not happy with it. So is there any other approach? pls help.

  • 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-13T01:27:23+00:00Added an answer on May 13, 2026 at 1:27 am

    I’m don’t think this will give you exactly what you want as the query will result in an Ienumerable object. Meaning where you want a single object with lots of title and text fields. This will return an object with title and text being objects within an Ienumerable object.

    so in your example you will have an object with two object, each of these anonymous objects now holding the title and text. once you have this, you can build up the xml the way you want. It’s not really that much different from your solution but as requested, it will give you one linq query to work with. Or at the very least give you an idea to build on.

        XElement Results = XElement.Load("c:\\test.xml"); //load you xml
        XNamespace NameSpace = Results.Name.Namespace;
        var xe = (from page in Results.Elements(NameSpace + "page")
                  let  title = page.Elements(NameSpace+"title")                      
                  let text = page.Elements(NameSpace+"text").Where(a=>a.Attribute("attrib1").Value.Equals("4"))   
                  where text.Count() > 0 
            //the where is simply to remove any unncessary data 
            //if there was ever a page that didn't have attrib1 = 4
    
                select new {title, text});
    

    Hope this, at least, gives you a couple new ideas

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

Sidebar

Related Questions

So I have some XML in the following format: <somenode> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title/>
I have an XML file which is in the following format: <root> <category> <doctype>
I have a xml document on the following format and want to transform it
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I have XML with a value like the following: <products> <product id=1 name=All Products>
I want XML in the following format: <configuration><!-- Only one configuration node --> <logging>...</logging><!--
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
I have the following xml that's sent to me from a web service. I'm
My question is the following. I have xml that is versioned by a namespace.

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.