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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:15:38+00:00 2026-05-29T06:15:38+00:00

I’m trying to write a piece of code which receives a string, uses the

  • 0

I’m trying to write a piece of code which receives a string, uses the data in this string to make changes to another string then saves the other string

I would prefer to do this using linq as I’m somewhat familiar with it, although that’s not to say that I’m completely close minded.

Anyway, the string being received is in a form like

"<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><Value><Code>AAA</Code><Description>First description</Description><Bool>Y</Bool></Value><Value><Code>BBB</Code><Description>Second description</Description><Bool>Y</Bool></Value><Value><Code>CCC</Code><Description>Third description</Description><Bool>N</Bool></Value></Root>";

or with proper formatting

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>
    <Root>
        <Value>
            <Code>AAA</Code>
            <Description>First description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>BBB</Code>
            <Description>Second description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>CCC</Code>
            <Description>Third description</Description>
            <Bool>N</Bool>
        </Value>
    </Root>"

and, for example. the other value is like

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>
    <Root>
        <Value>
            <Code>111</Code>
            <Description>111 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>AAA</Code>
            <Description>First description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>222</Code>
            <Description>222 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>BBB</Code>
            <Description>Second description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>333</Code>
            <Description>333 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>CCC</Code>
            <Description>Third description</Description>
            <Bool>Y</Bool>
        </Value>
    </Root>"

so of the same form but with more values and with all Bools set to Y. All I want to do is to find all the codes with a bool set to N and set those Bools on the new XML to N.

so the result of combining both of these would be the new xml but the Value with code CCC would have the Bool set as N. so:

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>
    <Root>
        <Value>
            <Code>111</Code>
            <Description>111 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>AAA</Code>
            <Description>First description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>222</Code>
            <Description>222 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>BBB</Code>
            <Description>Second description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>333</Code>
            <Description>333 description</Description>
            <Bool>Y</Bool>
        </Value>
        <Value>
            <Code>CCC</Code>
            <Description>Third description</Description>
            <Bool>N</Bool>
        </Value>
    </Root>"

To me it seems as though there should be an incredibly easy way to do this using Linq to XML but I’ve been working at it for a while now and my inexperience with XML seems to be showing as I’m having quite a bit of trouble with this.

Any help would be much appreciated.

Thanks

  • 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-29T06:15:38+00:00Added an answer on May 29, 2026 at 6:15 am

    Something like that?

    using System.Linq;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static XElement Join(XElement xmlOne, XElement xmlTwo)
            {
                return new XElement(
                    "Root",
                        xmlOne.Elements("Value").Concat(xmlTwo.Elements("Value")).GroupBy(element => element.Element("Code").Value).Select(
                            group =>
                                new XElement("Value",
                                    new XElement("Code", group.First().Element("Code").Value),
                                    new XElement("Description", group.First().Element("Description").Value),
                                    new XElement("Bool", group.Any(elem => elem.Element("Bool").Value == "N") ? "N" : "Y"))).ToArray());
    
            }
    
            static void Main(string[] args)
            {
                var xmlOne = XElement.Parse("<?xml version=\"1.0\" encoding=\"utf-8\"?>    <Root>        <Value>            <Code>AAA</Code>            <Description>First description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>BBB</Code>            <Description>Second description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>CCC</Code>            <Description>Third description</Description>            <Bool>N</Bool>        </Value>    </Root>");
                var xmlTwo = XElement.Parse("<?xml version=\"1.0\" encoding=\"utf-8\"?>    <Root>        <Value>            <Code>111</Code>            <Description>111 description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>AAA</Code>            <Description>First description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>222</Code>            <Description>222 description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>BBB</Code>            <Description>Second description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>333</Code>            <Description>333 description</Description>            <Bool>Y</Bool>        </Value>        <Value>            <Code>CCC</Code>            <Description>Third description</Description>            <Bool>Y</Bool>        </Value>    </Root>");
                var result = Join(xmlOne, xmlTwo);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.