简体   繁体   English

使用LINQ读取XML值

[英]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? 使用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;                
           }


        }

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

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. 您在foreach循环中分配的所有那些字符串变量的作用域仅是循环的一次迭代。 They are created and destroyed each time. 它们每次都被创建和销毁。

This may not work precisely in your case depending on the xml structure. 根据xml结构,这可能无法在您的情况下精确地工作。 Play around with it. 玩吧。 Try it using LinqPad 使用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
               };

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM