Nick Kahn
Nick Kahn

Reputation: 20078

Reading the XML values using LINQ

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; 
                ...............
            }

Upvotes: 0

Views: 612

Answers (3)

Amy B
Amy B

Reputation: 110111

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)
      )
    )
  ); 

Upvotes: 0

µBio
µBio

Reputation: 10748

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
               };

Upvotes: 0

Coding Flow
Coding Flow

Reputation: 21881

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.

Upvotes: 1

Related Questions