我有一个类CampaignRows
,其中有10个属性,如我的KeywordPerformanceReportColumns列中所述
我有一个XDocumet加载了以下xml
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://adcenter.microsoft.com/advertiser/reporting/v5/XMLSchema" >
<KeywordPerformanceReportColumns>
<Column name="CampaignName" />
<Column name="AdGroupName" />
<Column name="Keyword" />
<Column name="Devicetype" />
<Column name="Clicks" />
<Column name="Impressions" />
<Column name="Ctr" />
<Column name="AverageCpc" />
<Column name="ConversionRate" />
<Column name="GregorianDate" />
</KeywordPerformanceReportColumns>
<Table>
<Row>
<CampaignName value="sdfdsf October 4, 2011" />
<AdGroupName value="sdfsdf and sdf" />
<Keyword value="sdf sdf" />
<Devicetype value="Computer" />
<Clicks value="0" />
<Impressions value="7" />
<Ctr value="0.0000" />
<AverageCpc value="0.00" />
<ConversionRate value="" />
<GregorianDate value="4/1/2014" />
</Row>
<Row>
<CampaignName value="sdfdsf October 5, 2011" />
<AdGroupName value="sdfsdf and sdf" />
<Keyword value="sdf sdf" />
<Devicetype value="Computer" />
<Clicks value="0" />
<Impressions value="1" />
<Ctr value="0.0000" />
<AverageCpc value="0.00" />
<ConversionRate value="" />
<GregorianDate value="4/1/2014" />
</Row>
<Row>
<CampaignName value="sdfdsf October 6, 2011" />
<AdGroupName value="sdfsdf and sdf" />
<Keyword value="sdf sdf" />
<Devicetype value="Computer" />
<Clicks value="0" />
<Impressions value="7" />
<Ctr value="0.0000" />
<AverageCpc value="0.00" />
<ConversionRate value="" />
<GregorianDate value="4/1/2014" />
</Row>
</Table>
<Copyright>Abcd</Copyright>
</Report>
如何使用行节点中的每一行数据从XLinq查询中填充List
var person = from p in XDocument.Load(@"E:\526571836.xml").Descendants("Report").Descendants("Table").Descendants("Row")
select new Campaign { CampaignName = p.Element("CampaignName").Attribute("value").Value };
int icnt = person.Count();//having result = 0
答案 0 :(得分:0)
我可以通过以下代码
来完成XDocument doc = XDocument.Load(@"E:\526571836.xml");
XNamespace ns = doc.Root.Name.NamespaceName;
var campaignList = doc.Root.Element(ns + "Table").Elements(ns + "Row")
.Select(row => new CampaignObjectFromXml()
{
CampaignName = (string)row.Element(ns + "CampaignName").Attribute("value"),
AdGroupName = (string)row.Element(ns + "AdGroupName").Attribute("value"),
Keyword = (string)row.Element(ns + "Keyword").Attribute("value"),
Devicetype = (string)row.Element(ns + "Devicetype").Attribute("value"),
Clicks = (int)row.Element(ns + "Clicks").Attribute("value"),
Impressions = (int)row.Element(ns + "Impressions").Attribute("value"),
Ctr = (((string)row.Element(ns + "Ctr").Attribute("value")).Length > 0 ? (decimal?)row.Element(ns + "Ctr").Attribute("value") : null),
AverageCpc = (((string)row.Element(ns + "AverageCpc").Attribute("value")).Length > 0 ? (decimal?)row.Element(ns + "AverageCpc").Attribute("value") : null),
ConversionRate = (((string)row.Element(ns + "ConversionRate").Attribute("value")).Length > 0 ? (decimal?)row.Element(ns + "ConversionRate").Attribute("value") : null),
GregorianDate = XmlConvert.ToDateTime((string)row.Element(ns + "GregorianDate").Attribute("value"), @"M\/d\/yyyy")
}).ToList();
public class CampaignObjectFromXml
{
public string CampaignName { get; set; }
public string AdGroupName { get; set; }
public string Keyword { get; set; }
public string Devicetype { get; set; }
public int Clicks { get; set; }
public int Impressions { get; set; }
public decimal? Ctr { get; set; }
public decimal? AverageCpc { get; set; }
public decimal? ConversionRate { get; set; }
public DateTime GregorianDate { get; set; }
}