Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Merging and concatenating similiar records with linq to xml

Merging and concatenating similiar records with linq to xml

Scheduled Pinned Locked Moved LINQ
csharplinqcomxmlhelp
2 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Esulin2
    wrote on last edited by
    #1

    Here's another one I'm hoping you guys could help me with. I have an XML structure (example below) which is dynamically generated from a flat file. The sources vary, so the structure of the XML will differ from one execution to the next. What I need to achieve is to merge similar records in the XML data into one record, where some fields are merged, and some concatenated. The user specifies which field is the 'Key' field, and which one(s) is/are the concatenation fields. For example, in the following XML, the 'EMail' field is the key, while the 'ListDescription' is to be concatenated:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <SourceContactList_Root>
    <SourceContactList>
    <EMail>bobsmith@acme.com</EMail>
    <ListDescription>List Type 1</ListDescription>
    <Title>Mr.</Title>
    <Firstname>Bob</Firstname>
    <Surname>Smith</Surname>
    <JobTitle>Administration Manager</JobTitle>
    <CompanyName>Acme Inc</CompanyName>
    </SourceContactList>
    <SourceContactList>
    <EMail>bobsmith@acme.com</EMail>
    <ListDescription>List Type 2</ListDescription>
    <Title>Mr.</Title>
    <Firstname>Bob</Firstname>
    <Surname>Smith</Surname>
    <JobTitle>Administration Manager</JobTitle>
    <CompanyName>Acme Inc</CompanyName>
    </SourceContactList>
    <SourceContactList>
    <EMail>bobsmith@acme.com</EMail>
    <ListDescription>List Type 3</ListDescription>
    <Title>Mr.</Title>
    <Firstname>Bob</Firstname>
    <Surname>Smith</Surname>
    <JobTitle>Administration Manager</JobTitle>
    <CompanyName>Acme Inc</CompanyName>
    </SourceContactList>
    </SourceContactList_Root>

    The result I'm looking for is this:

    <SourceContactList>
    <EMail>bobsmith@acme.com</EMail>
    <ListDescription>List Type 1;List Type 2;List Type 3</ListDescription>
    <Title>Mr.</Title>
    <Firstname>Bob</Firstname>
    <Surname>Smith</Surname>
    <JobTitle>Administration Manager</JobTitle>
    <CompanyName>Acme Inc</CompanyName>
    </SourceContactList>

    Note that the 'ListDescription' field values for the three records where concatenated with a ; separator.

    J 1 Reply Last reply
    0
    • E Esulin2

      Here's another one I'm hoping you guys could help me with. I have an XML structure (example below) which is dynamically generated from a flat file. The sources vary, so the structure of the XML will differ from one execution to the next. What I need to achieve is to merge similar records in the XML data into one record, where some fields are merged, and some concatenated. The user specifies which field is the 'Key' field, and which one(s) is/are the concatenation fields. For example, in the following XML, the 'EMail' field is the key, while the 'ListDescription' is to be concatenated:

      <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      <SourceContactList_Root>
      <SourceContactList>
      <EMail>bobsmith@acme.com</EMail>
      <ListDescription>List Type 1</ListDescription>
      <Title>Mr.</Title>
      <Firstname>Bob</Firstname>
      <Surname>Smith</Surname>
      <JobTitle>Administration Manager</JobTitle>
      <CompanyName>Acme Inc</CompanyName>
      </SourceContactList>
      <SourceContactList>
      <EMail>bobsmith@acme.com</EMail>
      <ListDescription>List Type 2</ListDescription>
      <Title>Mr.</Title>
      <Firstname>Bob</Firstname>
      <Surname>Smith</Surname>
      <JobTitle>Administration Manager</JobTitle>
      <CompanyName>Acme Inc</CompanyName>
      </SourceContactList>
      <SourceContactList>
      <EMail>bobsmith@acme.com</EMail>
      <ListDescription>List Type 3</ListDescription>
      <Title>Mr.</Title>
      <Firstname>Bob</Firstname>
      <Surname>Smith</Surname>
      <JobTitle>Administration Manager</JobTitle>
      <CompanyName>Acme Inc</CompanyName>
      </SourceContactList>
      </SourceContactList_Root>

      The result I'm looking for is this:

      <SourceContactList>
      <EMail>bobsmith@acme.com</EMail>
      <ListDescription>List Type 1;List Type 2;List Type 3</ListDescription>
      <Title>Mr.</Title>
      <Firstname>Bob</Firstname>
      <Surname>Smith</Surname>
      <JobTitle>Administration Manager</JobTitle>
      <CompanyName>Acme Inc</CompanyName>
      </SourceContactList>

      Note that the 'ListDescription' field values for the three records where concatenated with a ; separator.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Ok, I'm not a Linq to XML expert, but here's my little attempt to get your "List Type 1; List Type 2; List Type 3" string:

      XDocument doc = XDocument.Load("Source.xml");
      var listDescriptions = from node in doc.DescendantNodes()
                            let element = node as XElement
                            where element != null && element.Name == "ListDescription"
                            select element.Value;

      var concatenatedListDescriptions = listDescriptions.Aggregate((in1, in2) => in1 + ";" + in2);

      After that, you've got your concatenated ListDescription. All that's left is writing the final document with this concatenated string in there. FWIW, I used this Linq to XML article[^] to figure out some stuff. Also, the Aggregate call that actually concats the strings together not optimal - ideally you'd have a StringBuilder that pieces them together.

      Life, family, faith: Give me a visit. From my latest post: "How differently the psalmist saw it! How blessed -- how truly happy with real joy! -- is the man who delights in the Law of the Lord." Judah Himango

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups