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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:36:43+00:00 2026-06-04T13:36:43+00:00

Sorry for this long post….But i have a headache from this task. I have

  • 0

Sorry for this long post….But i have a headache from this task.

I have a mile long xml document where I need to extract a list, use distinct values, and pass for transformation to web.

I have completed the task using xslt and keys, but the effort is forcing the server to its knees.

Description:
hundreds of products in xml, all with a number of named and Id’ed cattegories, all categories with at least one subcategory with name and id.

The categories are unique with ID, all subcategories are unique WITHIN that category:

Simplified example form the huge file (left our tons of info irrelevant to the task):

<?xml version="1.0" encoding="utf-8"?>
<root>
<productlist>
<product id="1">
<name>Some Product</name>
<categorylist>
<category id="1">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
<subcat id="2">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="2">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="3">
<name>cat1</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
</categorylist>
</product>
<product id="2">
<name>Some Product</name>
<categorylist>
<category id="1">
<name>cat1</name>
<subcategories>
<subcat id="2">
<name>subcat2</name>
</subcat>
<subcat id="4">
<name>subcat4</name>
</subcat>
</subcategories>
</category>
<category id="2">
<name>cat2</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
<category id="3">
<name>cat3</name>
<subcategories>
<subcat id="1">
<name>subcat1</name>
</subcat>
</subcategories>
</category>
</categorylist>
</product>
</productlist>
</root>

DESIRED RESULT:

<?xml version="1.0" encoding="utf-8"?>
<root>
<maincat id="1">
<name>cat1</name>
<subcat id="1"><name>subcat1</name></subcat>
<subcat id="2"><name>subcat2</name></subcat>
<subcat id="3"><name>subcat3</name></subcat>
</maincat>
<maincat id="2">
<name>cat2</name>
<subcat id="1"><name>differentsubcat1</name></subcat>
<subcat id="2"><name>differentsubcat2</name></subcat>
<subcat id="3"><name>differentsubcat3</name></subcat>
</maincat>
<maincat id="2">
<name>cat2</name>
<subcat id="1"><name>differentsubcat1</name></subcat>
<subcat id="2"><name>differentsubcat2</name></subcat>
<subcat id="3"><name>differentsubcat3</name></subcat>
</maincat>
</root>

(original will from 2000 products produce 10 categories with from 5 to 15 subcategories)

Things tried:

  1. Xslt with keys – works fine, but pooooor performance
  2. Played around with linq:

           IEnumerable<XElement> mainCats =
                    from Category1 in doc.Descendants("product").Descendants("category") select Category1;
    
                var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
                cDoc.Root.Add(mainCats);
                cachedCategoryDoc = cDoc.ToString();
    

    Result was a “categories only” (not distinct values of categories or subcategories)

Applied the same xlst to that, and got fairly better performance….. but still far from usable…

Can i apply some sort of magic with the linq statement to have the desired output??

A truckload of good karma goes out to the ones that can point me in det right direction..

//Steen

NOTE:

  • I am not stuck on using linq/XDocument if anyone has better options
  • Currently on .net 3.5, can switch to 4 if needed
  • 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-06-04T13:36:44+00:00Added an answer on June 4, 2026 at 1:36 pm

    If I understood your question corectly, here’s a LINQ atempt.

    The query below parses your XML data and creates a custom type which represents a category and contains the subcategories of that element.

    After parsing, the data is grouped by category Id to get distinct subcategories for each category.

    var doc = XElement.Load("path to the file");
    var results = doc.Descendants("category")
        .Select(cat => new
        {
            Id = cat.Attribute("id").Value,
            Name = cat.Descendants("name").First().Value,
            Subcategories = cat.Descendants("subcat")
                .Select(subcat => new
                {
                    Id = subcat.Attribute("id").Value,
                    Name = subcat.Descendants("name").First().Value
                })
         })
         .GroupBy(x=>x.Id)
         .Select(g=>new
         {
             Id = g.Key,
             Name = g.First().Name,
             Subcategories = g.SelectMany(x=>x.Subcategories).Distinct()
         });
    

    From the results above you can create your document using the code below:

    var cdoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root")); 
    cdoc.Root.Add(
        results.Select(x=>
        {
            var element = new XElement("maincat", new XAttribute("id", x.Id));
            element.Add(new XElement("name", x.Name));
            element.Add(x.Subcategories.Select(c=>
            {
                var subcat = new XElement("subcat", new XAttribute("id", c.Id));
                subcat.Add(new XElement("name", c.Name));
                return subcat;
            }).ToArray());
            return element;
        }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this long post. The question is however small but requires full detail.
I have this LINQ query (sorry it;s a long one) that query sets from
Sorry if this post appears to be long winded. I have a parent repeater
Sorry for the long post, but this forum always asks for use cases :-).
Hello (this is a long post sorry), I am writing a application in ASP.NET
First of all, sorry if I end up making a too long post, but
First of all Sorry for the really long post, now And this is my
I am sorry if the post is too long, but I would be happy
I apologize for the long post, but this problem is not easily stated. I
Long post.. sorry I've been reading up on this and tried back and forth

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.