2010-07-19 239 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