Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I have xml as below , want to read and assign it into list/dictionary

<resource id="laptops">
   <laptop>
     <code>1</code>
     <Description>abc1</Description>
     <NickName>xyz1</NickName>
   </laptop>
   <laptop>
      <code>2</code>
     <Description>abc2</Description>
     <NickName>xyz2</NickName>
   </laptop>
 </resource>


or

<resource id="laptops">
  <laptop id="1">
    <Description>abc1</Description>
    <NickName>xyz1</NickName>
  </laptop>

  <laptop id="2">
    <Description>abc2</Description>
    <NickName>xyz2</NickName>
  </laptop>
</resource>


What I have tried:

i tried something as below:

var result = from c in resourceNode.Descendants("laptops")
                                 select 
                    new FeesInfo()
                    {
                        FeeCode = (int)c!.Attribute("code")!,
                        FeeDescription = (string)c!.Attribute("Description")!,
                       Proposed = (string)c!.Attribute("NickName")!
                    };
Posted
Updated 15-Jan-23 12:34pm
v2

1 solution

An almost identical question was asked and answered 2 days ago. Rather that repost the same answer here, please click on the following link: View a XML file in xdocument and linq[^]
 
Share this answer
 
Comments
lalit.mca2006 16-Jan-23 0:51am    
Thanks Graeme_Grant, It helped me a lot. And could you please tell me which xml format should we use? as i mentioned 2 formats to store the same data.
Graeme_Grant 16-Jan-23 3:39am    
Glad to hear!
Maciej Los 16-Jan-23 15:27pm    
Do you mean with attributes or without? It's up to you.
Maciej Los 16-Jan-23 15:23pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900