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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:06:43+00:00 2026-05-21T19:06:43+00:00

I am using linqtoxml to analyse an existing XML file that is basically of

  • 0

I am using linqtoxml to analyse an existing XML file that is basically of the form

<Projects>
  <Project ProjectName="name1">
    <ObjectType1List>
      <ObjectType1 ProjectName="name1" Idx="1">
        <Location Top="104" Left="32" Height="64" Wdth="128" />
        ...
      </ObjectType1>

      <ObjectType1 ProjectName="name1" Idx="2">
        ...
      </ObjectType1>
    </ObjectType1List>

    <ObjectType2List>
      ...
    </ObjectType2List>
  </Project>

  <Project Name ="name2">
  </Project>
  ...
</Project>

Where there are multiple projects in the file and about 10 cannonical object types inside a project – although each project may or may not have all the different object types. But where a project does contain an objecttype, there will be a finite list of instances of that object type.

Now what I am trying to do is to find all objects of a given object type that are positioned on top of each other (and I know that I will only be interested in 2 of the object types, and I am assuming that only objects of the same type will be overlayed on each other). But the objects have been drawn and positioned by hand, so I can’t assume that such objects have exactly the same dimensions or position – but I will assume that they will match within some aribtray maximum size difference, so i have this feeling I need to scan and group objects by some sort of fuzzy requirement based on the center location of an object.

And the “can I have a pony too” request is that I am only interested in seeing more than one object on top of another. Within a list of a given object type, there could be a group of objects positioned on top of each other, as well as a bunch of other objects that do not overlap – I don’t want to know about those latter objects.

So for a file loaded by an XDocument I am thinking of a 2 pass approach like (and I am not sure if I have the correct syntax for Top et al) :

  var objects = from object in xdoc.Descendants()
    where object.Name = "ObjectType1" or object.Name = "ObjectType2" 
    select new
    {
      Project = object.Attribute("ProjectName").Value.ToString(),
      ObjectType = object.Name,
      Index = (int)object.Attribute("Idx").Value,
      Top = (int)object.Element("Location").Attribute("Top").Value,
      Left = (int)object.Element("Location").Attribute("Left").Value,
      Width = (int)object.Element("Location").Attribute("Width").Value,
      Height = (int)object.Element("Location").Attribute("Height").Value
    };


  var stacked = from object in objects
   group object by ???????

Its the ??????? bit that I don’t know how to write. I know I want to group by Project and ObjectType and then by some mathematical function of Top, Left, Width and Height. This is a twofold problem, as I know I don’t understand multiple groupings in linq, and then the comparision of objects will be something like:

abs(obj1.Left + obj1.Width/2 - obj2.Left - obj2.Width/2)<epsilon &&
abs(obj1.Top  + obj1.Height/2 - obj2.Top - obj2.Height/2)<epsilon

So any suggestions?

Note that while my current project is .net 3.5 I have no retrictions that would stop me going to 4.0

Edit1

From questions raised in Timwi’s answer. For any object type, I expect that there will be a bunch of objects that will be well located over each other but will not overlap with other objects not in the same bunch. So for objects A, B, C that overlap, You will not have A & B overlap, B & C overlap and A & C not overlapping.

Thus A will always overlap B and C, B will always overlap A & C and C will always overlap A & B. However there may be objects D, E and F which are located elsewhere which do not overlap A, B or C (or each other), but there may be a third (or more) group of objects G, H and I which overlap themselves.

So for a given project, and a given object type, and objects A,B,C (that overlap themselves), D, E and F (that do not overlap with any other object) and G, H and I (which overap themselves and no other objects as well) I’d like to see an output grouped like:

Project
  ObjectType1List
    Group1
      A, B, C
    Group2
      G, H, I

In the actual data there may be many such groups for a given Object type.

  • 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-21T19:06:44+00:00Added an answer on May 21, 2026 at 7:06 pm

    If your input consists entirely of disjoint groups of rectangles, and within each group every rectangle intersects with every other, then you need to compare each rectangle with at most one from each group. Thus:

    var groups = new Dictionary<Rectangle, List<Rectangle>>();
    foreach (var rectangle in input)
    {
        var key = groups.Keys.FirstOrDefault(k => k.IntersectsWith(rectangle));
        if (key.IsEmpty)
            groups[rectangle] = new List<Rectangle> { rectangle };
        else
            groups[key].Add(rectangle);
    }
    

    Since you mentioned you wanted to omit groups that have only single rectangles, you can just filter them out at the end:

    foreach (var pair in groups.Where(kvp => kvp.Value.Count == 1).ToList())
        groups.Remove(pair.Key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to append some nodes to an xml document using Linq2XML. The file
I have a string containing XML document using LinqtoXML What is the best way
Does anyone know how to generate an XSD using LinqToXml? I can't find any
I have a project with a formidable data access layer using LinqtoSQL for just
I'm writing an .NET 3.5 web application that is using LinqToSql for the basic
I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into
I currently have an existing database and I am using the LINQtoSQL generator tool
I'm engaged in writing a product using LinqToSql for data-access. While we're rapidly developing,
I just started creating my data-access layer using LinqToSql. Everybody is talking about the
I am currently building a web site and I just implemented SqlCacheDependency using LinqToSQL

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.