2010-07-19 90 views
0

什麼是使用linq和下面的代碼讀取xml文件的最佳方式,您將看到,我有三個不同的循環,我覺得它不夠優雅,或者我有選項來改裝下面的碼?使用LINQ讀取XML值

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


     } 

更新:

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

你的代碼是做什麼的? – dtb 2010-07-19 17:25:31

+0

是否有一個原因,你想使用Linq而不是簡單地將它反序列化成一個類? – 2010-07-19 17:25:53

+0

@Mark:沒有理由使用Linq,如果你有更好的方法,請讓我知道。 – 2010-07-19 17:28:47

回答

0

根據xml結構,這可能不適用於您的情況。玩它。嘗試使用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 
       }; 
1

那是你的實際代碼?所有在foreach循環中分配的字符串變量只有循環的一次迭代範圍。每次創建和銷燬它們。

+0

這是正確的,我已經做了測試目的... – 2010-07-19 17:32:31

0

此答案是分層查詢。

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

標題來自哪裏? 選擇新的XElement(「標題」, – 2010-07-19 18:54:50

+0

標題是在上面的行中引入的...從標題中我從頭到尾 – 2010-07-19 19:06:14

+0

我已經udpate我的問題,有兩個問題在您的解決方案 1)它不循環 2)它確實不讀取ExpirationDate或FullName ... – 2010-07-19 19:37:15