Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ读取XML值_C#_Xml_Linq - Fatal编程技术网

C# 使用LINQ读取XML值

C# 使用LINQ读取XML值,c#,xml,linq,C#,Xml,Linq,使用linq和下面的代码读取xml文件的最佳方法是什么?您将看到,我有三个不同的循环,我觉得它并不优雅,或者我是否可以选择修改下面的代码 public static void readXMLOutput(Stream stream) { XDocument xml = new XDocument(); xml = LoadFromStream(stream); var header = from p in x

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

这是你的真实代码吗?在foreach循环中分配的所有字符串变量的作用域仅为循环的一次迭代。它们每次都会被创建和销毁。

根据xml结构的不同,这在您的情况下可能无法精确地工作。玩玩它。试着用


这个答案是一个分层查询

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

您想使用Linq而不是简单地将其反序列化到类中有什么原因吗?@Mark:没有理由使用Linq,如果您有更好的方法,请告诉我。@dtb:它从输入流读取xml。。。这很简单,没什么特别的。标题是从哪里来的?选择new XElement(“Header”,在上面的一行中引入了Header…从Header ini中有udpate my question,解决方案中有两个问题1)它不循环2)它不读取ExpirationDate或FullName…为什么循环不成问题?它读取最后一行中的到期日期和全名。
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)
      )
    )
  );