Linq to Xml:复杂的查询帮助

时间:2009-07-20 19:58:28

标签: c# linq-to-xml

假设我有以下XML数据:

<?xml version="1.0" encoding="utf-8" ?>
<Accounts>
    <Account name="Account1">
        <Campaign name="Camp1">
            <RemoteCampaign>RC1</RemoteCampaign>
            <RemoteCampaign>RC2</RemoteCampaign>
        </Campaign>
        <Campaign name="Camp2">
            <RemoteCampaign>RC3</RemoteCampaign>
        </Campaign>

    </Account>
    <Account name="Account2">
        <Campaign name="Camp3">
            <RemoteCampaign>RC4</RemoteCampaign>
        </Campaign>
        <Campaign name="Camp4">
            <RemoteCampaign>RC5</RemoteCampaign>
        </Campaign>

    </Account>
</Accounts>

我需要在给定帐户和远程广告系列名称时确定广告系列名称。在Linq to Xml中有一个简单的方法吗?可以假设所有值都是唯一的。

3 个答案:

答案 0 :(得分:2)

以下可行:

var query = from aa in xdoc.Descendants("Account")
            where    aa.Attribute("name") != null
                  && aa.Attribute("name").Value == accountName
            from cc in aa.Descendants("Campaign")
            where    cc.Attribute("name") != null
                  && cc.Descendants("RemoteCampaign").Any(elt => elt.Value == remoteName)
            select cc.Attribute("name").Value;

答案 1 :(得分:0)

public static string GetCampaignName(string xml, string accountName, string rcName)
{
    return XDocument.Parse(xml).Descendants("Account")
        .Where(a => string.Equals(a.Attribute("name").Value,accountName)).Descendants("Campaign")
        .Where(c => c.Descendants("RemoteCampaign").Select(rc => rc.Value).Contains(rcName))
        .First().Attribute("name").Value;
}

上述函数假定每个Campaign都有一个名称,否则将抛出NullReferenceException;因此,如果您认为并非所有广告系列都有名称,请将其拆分并检查是否为空。

答案 2 :(得分:0)

这有效,但效率可能不高:

        XDocument xml = XDocument.Load(Server.MapPath("XMLFile.xml"));
    string account = "Account1";
    string remoteCampaign = "RC1";
    string campaign = xml.Descendants()
        .Where(rc => rc.Value == remoteCampaign && rc.Ancestors("Account").Any(a => a.Attribute("name").Value == account))
        .Where(n => n.Parent.Name == "Campaign")
        .Select(c => c.Parent.Attribute("name").Value).FirstOrDefault();