开发者

Xml file reading using XML to LINQ in C#

I've an xml file (Sample.xml) which has the following structure

<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" 开发者_Python百科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> 

i need to read the attribute values of the element" Child" which is directly under "RootElement"

Now i'm using the LINQ query like

          List<string> lst = (from element in XDocument.Load(Application.StartupPath + "\\Sample.xml")
                                                       .Descendants("Child")
                                                        group element by element.Attribute("Name").Value into KeyGroup select KeyGroup.Key )
                                                       .ToList();

but it returns the attribute value of "Name" of all the "Child" element in the file.

Please give me a better solution to do this using XML to LINQ

Thanks in advance


Use something like this:

XDocument doc = XDocument.Load(Application.StartupPath + "\\Sample.xml");
List<string> lst = doc.Root.Elements("Child")
                      .Select(x => (string) x.Attribute("Name"))
                      .ToList();

Note the use of Elements() instead of Descendants(). That will get you all the name attributes. It's unclear to me why you were grouping in your original sample code, but hopefully this will get you going.


You grouped by name, so your list would have name elements. You may query and iterate your results as follows:

var query = from element in XDocument.Load(Application.StartupPath + "\\Sample.xml")
    .Descendants("Child")
    group element by element.Attribute("Name").Value into KeyGroup 
    select new { Grouping = KeyGroup.Key, Children = KeyGroup };

and the iteration

foreach(var namekey in query)
{
    Console.WriteLine(namekey.Grouping);
    foreach(var child in namekey.Children)
    {
        Console.WriteLine(child.whatever);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜