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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:21:03+00:00 2026-05-15T13:21:03+00:00

I’ve an xml file Sample.xml <RootElement> <Children> <Child Name=FirstChild Start=0 End=2> <Sibling Name=Test1 />

  • 0

I’ve an xml file “Sample.xml”

  <RootElement>
    <Children>
       <Child Name="FirstChild" Start="0" End="2">
          <Sibling Name="Test1" />
          <Sibling Name="Test2" />
          <AdditionalSibling Name="Add_Test_1" />
          <AdditionalSibling Name="Add_Test_2" />
          <MissingSibling Name="Miss_Test_1" />
          <MissingSibling Name="Miss_Test_2" /
     </Child>

     <Child Name="SecondChild" Start="0" End="2">
          <Sibling Name="Test3" />
          <Sibling Name="Test4" />
     </Child>
     <Child Name="ThirdChild" Start="0" End="2">
          <Sibling Name="Test5" />
          <Sibling Name="Test6" />
      </Child>
      <Child Name="FourthChild" Start="0" End="2">
          <Sibling Name="Test7" />
          <Sibling Name="Test8" />
      </Child>
      <Child Name="FifthChild" Start="0" End="2">
          <Sibling Name="Test9" />
          <Sibling Name="Test10" />
      </Child>
      <Child Name="SixthChild" Start="0" End="2">
          <Sibling Name="Test11" />
          <Sibling Name="Test12" />
      </Child>

      <MatchedChilds>
         <Child Name="FirstChild" />
         <Child Name="SecondChild" />
         <Child Name="ThirdChild" />
         <Child Name="FourthChild" />
         <Child Name="FifthChild" />
         <Child Name="SixthChild" /> 
     </MatchedChilds>
   </Children>
 </RootElement>

And a Class “SampleClass”

public class SampleClass
{
    string Start;
    string End;
    List<string> Siblings;
    List<string> AdditionalSiblings;
    List<string> MissingSiblings;
     public SampleClass()
     {
        Start= "";
        End = "";
        Siblings = new List<string>();
        AdditionalSiblings = new List<string>();
        MissingSiblings = new List<string>();
     }
    public SampleClass( string St, string En,List<string> S, List<string> AS, List<string> MS)
    {
        Start= St;
        End = En;
        Siblings = S;
        AdditionalSiblings = AS;
        MissingSiblings = MS;
    }

}

and in another class i’ve declared a Dictonary Like

 Dictionary<string, SampleClass> m_dictSample = new Dictionary<string, SampleClass>();

i need to fill this dictonary with the contents of the file ..

I’m using Xml Linq for this..

XDocument l_XDOC = XDocument.Load(Application.StartupPath + "\\Sample.xml");

m_dictSample = (from element in l_XDOC.Descendants("Child")
                        group element by element.Attribute("Name").Value into KeyGroup
                        select KeyGroup )
                        .ToDictionary(grp => grp.Key,
                                      grp => new
                                          SampleClass(grp.Attributes("Start").ToList()[0].Value.ToString(),
                                          grp.Attributes("End").ToList()[0].Value.ToString(),
                                          grp.Descendants("Sibling").Attributes("Name").Select(l_Temp => l_Temp.Value).ToList(),
                                          grp.Descendants("AdditionalSibling").Attributes("Name").Select(l_Temp => l_Temp.Value).ToList(),
                                          grp.Descendants("MissingSibling").Attributes("Name").Select(l_Temp => l_Temp.Value).ToList()));

This query is working properly for the above described file.

But if the file have more than one Element with same name, or an element with no “start” and “end” attribute make an exception while
executing the query.

I’ve problem with the following lines

     grp.Attributes("Start").ToList()[0].Value.ToString(),
     grp.Attributes("End").ToList()[0].Value.ToString()

please give me a better way to do this

And i need to fill a listView with the contents of the Dictonary like

S.no       Child              Siblings                Additional Siblings            Missing Siblings

 1         FirstChild         Test1,Test2             Add_Test_1,Add_Test_2           Miss_Test_1,Miss_Test_2

 2         SecondChild        Test3,Test4

 3         ThirdChild         Test5,Test6

now i’m using for loop for this

please give me a better way to do this..

  • 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-15T13:21:03+00:00Added an answer on May 15, 2026 at 1:21 pm

    Given the way that you have written your query I don’t see why you’d be getting an exception if you had two Child elements with the same name. If anything their data would just get combined into a single key instance in your dictionary. To fix the exception you receive when a Start attribute doesn’t exist just do a conditional operator test ? : to see if results were returned from your attribute query. That said the below code should work for you with the disclaimer that just because this works doesn’t mean it’s best practice. In many ways I am a LINQ neophyte myself.

    Dictionary<string, SampleClass> dict =
        (from element in xDoc.Descendants("Child")
                  group element by element.Attribute("Name").Value
                      into kGrp
                      select kGrp)
                .ToDictionary(grp => grp.Key,
                grp => new SampleClass
                {
                    Start = grp.Attributes("Start").Count() > 0
                            ? grp.Attributes("Start")
                                 .ToList()[0].Value.ToString()
                            : String.Empty
                    ,End = grp.Attributes("End").Count() > 0
                            ? grp.Attributes("End")
                                 .ToList()[0].Value.ToString()
                            : String.Empty
                    ,Siblings =
                        grp.Descendants("Sibling")
                            .Attributes("Name")
                            .Select(l_Temp => l_Temp.Value).ToList()
                    ,AdditionalSiblings =
                        grp.Descendants("AdditionalSibling")
                            .Attributes("Name")
                            .Select(l_Temp => l_Temp.Value).ToList()
                    ,MissingSiblings =
                        grp.Descendants("MissingSibling")
                            .Attributes("Name")
                            .Select(l_Temp => l_Temp.Value).ToList()
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.