tags:

views:

74

answers:

3

what is the best way of reading xml file using linq and the below code you will see that, I have three different loops and I feel like its not elegant or do I have options to retrofit the below code?

public static void readXMLOutput(Stream stream)
       {  
           XDocument xml = new XDocument();
           xml = LoadFromStream(stream); 

           var header = from p in xml.Elements("App").Elements("Application") 
                       select p;

           foreach (var record in header)
           {
               string noym = record.Element("nomy").Value;
               string Description = record.Element("Description").Value;
               string Name = record.Element("Name").Value;
               string Code = record.Element("Code").Value; 
           }

           var appRoles = from q in xml.Elements("App").Elements("Application").Elements("AppRoles").Elements("Role")
                        select q;

           foreach (var record1 in appRoles)
           {
               string Name = record1.Element("Name").Value;
               string modifiedName = record1.Element("ModifiedName").Value; 
           }

           var memeber = from r in xml.Elements("App").Elements("Application").Elements("AppRoles").Elements("Role").Elements("Members")
                          select r;

           foreach (var record2 in memeber)
           {

               string ExpirationDate = record2.Element("ExpirationDate").Value;
               string FullName = record2.Element("FullName").Value;                
           }


        }

UPDATED:

 foreach (var record in headers)
            {
                ..............
                string Name1 = record.Attribute("Name").Value;
                string UnmodifiedName = record.Attribute("UnmodifiedName").Value;

                string ExpirationDate = record.Attribute("ExpirationDate").Value;
                string FullName = record.Attribute("FullName").Value; 
                ...............
            }
+1  A: 

Is that your actual code ? All those string variables you are assigning in the foreach loops only have a scope of one iteration of the loop. They are created and destroyed each time.

Ben Robinson
@Ben, thats correct and i have done for testing purpose...
Abu Hamzah
A: 

This may not work precisely in your case depending on the xml structure. Play around with it. Try it using LinqPad

var applications = from p in xml.Descendants("Application") 
             select new { Nomy = p.Element("nomy").Value
                        , Description = p.Element("Description").Value 
                        , Name = p.Element("Name").Value
                        , Code = p.Element("Code").Value
             };

var appRoles = from r in xml.Descendants("Role")
               select new { Name = r.Element("Name").Value
                          , ModifiedName = r.Element("ModifiedName").Value
               };
BioBuckyBall
A: 

This answer is a hierarchical query.

var headers =
  from header in xml.Elements("App").Elements("Application")  
  select new XElement("Header",
    new XAttribute("noym", header.Element("nomy").Value),
    new XAttribute("Description", header.Element("Description").Value),
    new XAttribute("Name", header.Element("Name").Value),
    new XAttribute("Code", header.Element("Code").Value),
    from role in header.Elements("AppRoles").Elements("Role")
    select new XElement("Role",
      new XAttribute("Name", role.Element("Name").Value),
      new XAttribute("ModifiedName", role.Element("ModifiedName").Value),
      from member in role.Elements("Members")
      select new XElement("Member",
        new XAttribute("ExpirationDate", member.Element("ExpirationDate").Value),
        new XAttribute("FullName", member.Element("FullName").Value)
      )
    )
  ); 
David B
where does the Header come from?select new XElement("Header",
Abu Hamzah
header is introduced in the line above that... from header in
David B
i have udpate my question, there are two problems in your solution1) it does not loop2) it does not read the ExpirationDate or FullName...
Abu Hamzah
Why is not looping a problem? it reads the expiriation date and full name in the last line.
Matt Ellen
Your code is awesome, why do you need my code?
David B