简体   繁体   English

Linq关于复杂XML

[英]Linq on Complex XML

Hi I Have the following XML 嗨,我有以下XML

<Feed>
<Control>
        <Username>Fnol13</Username>
        <Password>12345</Password>          
</Control>
<Repairer>
        <RepairerName>Test</RepairerName>
        <RepairerAddress>Test</RepairerAddress>
        <RepairerTelephone>Test</RepairerTelephone>
</Repairer>
</Feed>

And a Model Class Containing following Properties 还有一个包含以下属性的模型类

[Serializable]
public  class Job {

    public string Username { get; set; }

    public string Password { get; set; }

    public string Reference { get; set; }

    public string RepairerAddress { get; set; }

    public string RepairerTelephone { get; set; }


}   

I am using following Linq Query to Extract Data from XML 我正在使用以下Linq查询从XML提取数据

var results = from job in xmlDoc.Descendants("Control")
                      select new Job {
                          Username = (string)job.Element("Username").Value,
                          Password = (string)job.Element("Password").Value


                      };
// Here I want to add as well Descendants("Repairer") using same query

        return results.ToList();

Problem is that can return Descendants("Control") However I would like to get also Descendants("Repairer") and return in a same list as my model is showing. 问题是可以返回Descendants(“ Control”),但是我也想获得Descendants(“ Repairer”),并在与模型显示相同的列表中返回。 Could you please help me to write Linq Query and I am confirming you I am very new in Linq. 您能帮我写Linq Query吗,我确认您是Linq的新人。

Your Model Job looks confusing to me because you may have multiple Control & Repairer nodes in which case it should map to a collection. 您的模型Job看起来让我感到困惑,因为您可能有多个“ ControlRepairer节点,在这种情况下,它应该映射到一个集合。 Anyways for current XML I assume you need the elements of first Repairer node, you can achieve it like this:- 无论如何,对于当前的XML我都假定您需要第一个Repairer节点的元素,您可以这样实现:

var results = from job in xmlDoc.Root.Elements("Control")
              let Repairer = job.Parent.Elements("Repairer").FirstOrDefault()
              select new Job
                   {
                      Username = (string)job.Element("Username"),
                      Password = (string)job.Element("Password"),
                      RepairerName = (string)Repairer.Element("RepairerName"),
                      RepairerAddress = (string)Repairer.Element("RepairerAddress"),
                      RepairerTelephone = (string)Repairer.Element("RepairerTelephone")
                   };

Also, note (string)job.Element("Username") will give you the node value. 另外,请注意(string)job.Element("Username")将为您提供节点值。 There is no need to call the Value property. 无需调用Value属性。

Update: 更新:

You can use XDocument when working with LINQ-to-XML:- 使用LINQ-to-XML时可以使用XDocument

XDocument xmlDoc = XDocument.Load(XMLpath);

您可以做类似的事情-

Reference = (string)job.Parent.Descendants("Repairer").FirstOrDefault().Element("RepairerAddress").Value;

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

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