使用LINQ读取XML值

时间:2010-07-19 17:13:11

标签: c# xml linq

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

3 个答案:

答案 0 :(得分:1)

这是你的实际代码吗?您在foreach循环中分配的所有字符串变量仅具有循环的一次迭代的范围。它们每次都被创造和销毁。

答案 1 :(得分: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
               };

答案 2 :(得分: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)
      )
    )
  );