0

CampaignRows我的KeywordPerformanceReportColumns 列中提到了一个具有 10 个属性的类

我有一个加载了以下 xml 的 XDocumet

<?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 查询中的列表

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
4

1 回答 1

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; }
    }
于 2013-07-12T10:51:30.247 回答