通过C#LINQ提取XML的详细记录

本文关键字:记录 XML C#LINQ 提取 通过 | 更新日期: 2023-09-27 18:24:54

我有以下XML摘录。提取XML的第一步没有问题,但我不知道如何进入第二层并提取每一层。具体来说,就是下面XML中的用户信息。

任何帮助都将不胜感激。。。

<?xml version="1.0" encoding="UTF-8" ?> 
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:getStatusReportResponse xmlns:ns2="http://xxxxxxxxx.com/">
      <return>
        <statusReport>
        <record>
          <assessorDate /> 
          <assessorOffice /> 
          <availableDate /> 
          <awardDate>01/01/2014</awardDate> 
          <awardValue>1000000</awardValue> 
              <businessSector>SYSTEMS</businessSector> 
          <user>
            <accessGrantedDate /> 
            <emailAddress>john.usda@noemail.mil</emailAddress> 
            <name>JOHN USDA</name> 
            <phoneNumber>XXX-XXX-XXXX</phoneNumber> 
            <role>Focal Point</role> 
          </user>
          <user>
            <accessGrantedDate /> 
            <emailAddress>john.usda@noemail.mil</emailAddress> 
            <name>JOHN USDA</name> 
            <phoneNumber>XXX-XXX-XXXX</phoneNumber> 
            <role>Focal Point</role> 
          </user>
        </record>
       </statusReport>
      </return>
    </ns2:getStatusReportResponse>
    </S:Body>
  </S:Envelope>

我试过这个,但它只给我一个第一个用户记录的列表,而不是所有的。

var records = from x in xml.Descendants("record")
              select new
              {
                  awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
                  ,userList = (List<string>) x.Descendants("user").Elements()                                                                //.Elements("accessGrantedDate")
                                                                  .Select(a => a.Value).ToList()
              };

通过C#LINQ提取XML的详细记录

我假设这是您的User类的模型:

public class User
{
    public DateTime? AccessGrantedDate { get; set; }
    public string EMailAddress { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public string Role { get; set; }
}

这是从XML:中提取User

var records = from x in xml.Descendants("record")
              select new
              {
                 AwardDate = (string)x.Element("awardDate"),
                 UserList = x.Descendants("user").Select(user => new User 
                 {
                    AccessGrantedDate = string.IsNullOrEmpty((string)user.Element("accessGrantedDate")) ? 
                                        (DateTime?) null : DateTime.Parse((string)user.Element("accessGrantedDate")),
                    EMailAddress = (string)user.Element("emailAddress"),
                    Name = (string)user.Element("name"),
                    PhoneNumber = (string)user.Element("phoneNumber"),
                    Role = (string)user.Element("role")
                 })
              };

据我所知,您的代码获取文件中所有用户的数据,但仅获取节点的值。

更有组织的方法是:

class User
    {
        public string accessGrantedDate { get; set; }
        public string emailAddress { get; set; }
        public string name { get; set; }       
        public string phoneNumber { get; set; }
        public string role { get; set; }       
    }

然后:

var records = from x in xml.Descendants("record")
              select new
              {
                  awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
                  ,userList = x.Descendants("user").Select(a=>new User
                  {
                        accessGrantedDate= a.Element("accessGrantedDate").Value,
                        emailAddress=a.Element("emailAddress").Value,
                        name=a.Element("name").Value,
                        phoneNumber=a.Element("phoneNumber").Value,
                        role = a.Element("role").Value
                  }).ToList()
              };

或者,如果你不想以这种方式组织数据,你可以使用列表列表:

 var records = from x in xml.Descendants("record")
        select new
        {
            awardDate = (string)x.Descendants("awardDate").FirstOrDefault().Value,
            userList = x.Descendants("user").Select(a => a.Elements().Select(b=>b.Value).ToList()).ToList()
        };

我通过使用第二个详细记录查询解决了这个问题,因此,在外部列表中创建了一个List作为对象。

        var records = from x in xml.Descendants("record")
                      select new
                      {
                          awardDate = x.Element("awardDate")
                          ,
                          userList = from u in x.Descendants("user")
                                     select new
                                       {
                                           accessGrantedDate = (string) u.Element("accessGrantedDate")
                                           ,emailAddress =(string) u.Element("emailAddress"
                                           ,name = (string) u.Element("name")
                                           ,phoneNumber = (string) u.Element("phoneNumber")
                                           ,role = (string) u.Element("role")
                                       }
                      };