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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:43:13+00:00 2026-05-24T11:43:13+00:00

I have a XML file that I’m trying to group by the attribute Width.

  • 0

I have a XML file that I’m trying to group by the attribute “Width”. Here is a snippet:

<nodes>
<FieldType Name="1000" OriginalName="1000" ScriptName="" SqlType="12" Width="1000" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="Varchar 1000" OriginalName="Varchar1000" ScriptName="" SqlType="12" Width="1000" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="Varchar 10001" OriginalName="Varchar1000" ScriptName="" SqlType="12" Width="1000" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="2000" OriginalName="1000" ScriptName="" SqlType="12" Width="200" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="Varchar 200" OriginalName="Varchar1000" ScriptName="" SqlType="12" Width="200" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="Varchar 2001" OriginalName="Varchar1000" ScriptName="" SqlType="12" Width="200" EnableValues="0" Scale="0" ForceMatch="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />
<FieldType Name="Y/N" ScriptName="" SqlType="12" Width="1" EnableValues="1" ForceMatch="1" Scale="0" ForceMatchCaseSensitive="0" SortAlphabetically="0" />

I want my parse to return 2 values, 1000 and 2000 since I want the unique widths. The code I wrote is:

            XDocument xmlDoc = XDocument.Load(@"c:\temp\sample.xml");
        var q = from c in xmlDoc.Descendants("FieldType")
                group xmlDoc by c.Attribute("Width") into cust_widths
                select new
                {
                    key = cust_widths.Key,
                    value = from val in cust_widths.Elements("Width") select (string)val
                };

        foreach (var name in q)
        {
            System.Diagnostics.Debug.WriteLine(name);
        }

But that still returns: 1000, 1000, 1000, 200, 200, 200.

Any idea what’s wrong with my syntax?

  • 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-24T11:43:17+00:00Added an answer on May 24, 2026 at 11:43 am

    Try this:

    XDocument xmlDoc = XDocument.Load(@"c:\temp\sample.xml");
    var q = 
        from c in xmlDoc.Descendants("FieldType")
        group c by c.Attribute("Width").Value into cust_widths
        select cust_widths.Key;
    
    foreach (var name in q)
    {
        System.Diagnostics.Debug.WriteLine(name);
    }
    

    Your code had a few issues:

    1. You were grouping the entire xmlDoc into the groupings instead of just the FieldType elements.
    2. You were grouping by XAttribute objects instead of by the attributes’ values.

    There’s actually an even simpler version of the query:

    var q = xmlDoc
        .Descendants("FieldType")
        .Select(c => c.Attribute("Width").Value)
        .Distinct();
    
    • 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 looks like: <sizes> <size name=original width=386 height=429> http://userserve-ak.last.fm/serve/_/62380831/Adele+PNG.png
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have an XML file that has a number of nodes, each of which
I have an XML file that is very long, but here is a shot
I have an XML file that I'm trying to serialize into an object. Some
I have an XML file that I want to sort by an attribute. The
I have a XML file that looks like this <Licence> <Name>Test company</Name> <Version>1.1.1.1</Version> <NumberOfServer>2</NumberOfServer>
I have an xml file that looks like the following. What I'm trying to
I have an XML file that I am trying to search using Java. 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.