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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:29:07+00:00 2026-05-31T11:29:07+00:00

I got an XML file that is 9MB large. Apparently, it is broken. I

  • 0

I got an XML file that is 9MB large. Apparently, it is broken.

I want to check if on any level 2 sibling elements have an attribute “Id” with same value.

Currently it goes too slow. What kind of optimizations I could make to this code?

Edited to include some tips

namespace ConsoleApplication1{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Xml.Linq;
  internal class Program{
    private const string _pathToXml=@"C:\4\4";
    private static readonly List<object> _duplicateLeafs=new List<object>();
    private static void Main(){
      var xml=ReadXml();
      var elements=xml.Descendants();
      foreach(var element in elements)
        FindDupes(element);
      Console.ReadLine();
      Debugger.Break();
    }
    private static XDocument ReadXml(){
      return XDocument.Parse(File.ReadAllText(_pathToXml));
    }
    private static void FindDupes(XElement element){
      var elements=element.Descendants();
      var elementsWithIds=elements.Where(x=>x.Attribute("Id")!=null);
      var ids=elementsWithIds.Select(x=>x.Attribute("Id")).ToList();
      for(var i=0;i<ids.Count;i++)
        for(var j=i+1;j<ids.Count;j++)
          if(i!=j&&ids[i]==ids[j])
            _duplicateLeafs.Add(elementsWithIds.First(x=>x.Attribute("Id")==ids[i]));
      foreach(var subElement in elements)
        FindDupes(subElement);
    }
  }
}
  • 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-31T11:29:08+00:00Added an answer on May 31, 2026 at 11:29 am

    You say you want to check level 2 descendants, but yet FindDupes is recursive, so you’re recursively checking two levels deep in foreach loops, every call.

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

Sidebar

Related Questions

I have an XML file that is loaded into an XElement. I want to
I have a very large XML file that I need to parse so I
I got a simple XML file that I want to validate against an XSD.
I have got a xml file and xsl file linking to it,that works fine.Im
I've got an XML file that I want to turn in to a list
I've got an XML file that contains a list of questions. I'd like to
I've got a PHP page that parses an XML file with SimpleXml, then passes
I've got an XML file on my server, and my partners have to access
A beginners question, hopefully easily answered. I've got an xml file I want to
I've got a XML Schema (xsd file) snippet bellow, in which I want the

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.