none
Wanted: Help translating VB LINQ to XML to C# RRS feed

  • Question

  • I want to load to XML objects (XElements, I think? Or XDocuments -- I'm not sure) from strings and perform a join. The following VB.NET code works thanks to some kind help from someone in another forum a while ago. I'm having a heck of a time getting the C# LINQ "from" and "select" statements to work with XML inputs. Many of the C#/LINQ code examples I found bing searching give me syntax errors in VS2010. Visual studio suggests I don't have a reference to System.xml.linq but I do!

     

    Could someone help me translate this into C#?

     

      Dim pets =
         <pets>
           <pet>
             <name>Barley</name>
             <owner>terry</owner>
           </pet>
           <pet>
             <name>boots</name>
             <owner>terry</owner>
           </pet>
           <pet>
             <name>whiskers</name>
             <owner>charlotte</owner>
           </pet>
           <pet>
             <name>blue moon</name>
             <owner>terry</owner>
           </pet>
           <pet>
             <name>daisy</name>
             <owner>magnus</owner>
           </pet>
         </pets>
        Dim persons = <persons>
                 <person>
                   <first>magnus</first>
                   <last>hedlund</last>
                 </person>
                 <person>
                   <first>terry</first>
                   <last>adams</last>
                 </person>
                 <person>
                   <first>charlotte</first>
                   <last>weiss</last>
                 </person>
                 <person>
                   <first>arlene</first>
                   <last>huff</last>
                 </person>
               </persons>
    
    
        Dim petOwners As XElement = <PetOwners>
                        <%= From person In persons.<person>
                          Group Join pet In pets.<pet> On person.<first>.Value Equals pet.<owner>.Value Into personsPets = Group
                          Select <Person FirstName=<%= person.<first>.Value %> LastName=<%= person.<last>.Value %>>
                                <%= From p In personsPets
                                  Select <pet><%= p.<name>.Value %></pet>
                                %>
                              </Person>
                        %>
                      </PetOwners>
    
    


    siegfried heintze
    Wednesday, July 20, 2011 11:23 PM

Answers

  • Hi Siegfried;

    This will do the same as the VB version.

     

    var pets = new XElement("pets",
        new XElement("pet",
         new XElement("name", "Barley"),
         new XElement("owner", "Terry")),
        new XElement("pet",
         new XElement("name", "Boots"),
         new XElement("owner", "Terry")),
        new XElement("pet",
         new XElement("name", "Whiskers"),
         new XElement("owner", "Charlotte")),
        new XElement("pet",
         new XElement("name", "Blue Moon"),
         new XElement("owner", "Terry")),
        new XElement("pet",
         new XElement("name", "Daisy"),
         new XElement("owner", "Magnus")));
    
    var persons = new XElement("persons",
        new XElement("person",
         new XElement("first", "Magnus"),
         new XElement("last", "Hedlund")),
        new XElement("person",
         new XElement("first", "Terry"),
         new XElement("last", "Adams")),
        new XElement("person",
         new XElement("first", "Charlotte"),
         new XElement("last", "Weiss")),
        new XElement("person",
         new XElement("first", "Arlene"),
         new XElement("last", "Huff"))); 
    

    XElement petOwners = new XElement("PetOwners", from person in persons.Elements("person") join pet in pets.Elements("pet") on person.Element("first").Value equals pet.Element("owner").Value into personsPets select new XElement("Person", new XAttribute("FirstName", person.Element("first").Value), new XAttribute("LastName", person.Element("last").Value), from p in personsPets select new XElement("pet", p.Element("name").Value)));

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by siegfried_ Thursday, July 21, 2011 10:38 PM
    Thursday, July 21, 2011 3:53 PM