在运行时vb.net将元素添加到xdocument

时间:2015-12-16 17:11:31

标签: c# vb.net .net-4.0 linq-to-xml .net-4.5

我有两个程序,

一个是用C#.NET V4.5编写的Web服务,另一个是用VB.NET V4.0编写的

我正在使用xDocument类创建一个xml文档以发送到web服务,

在VB程序中我有这段代码:

 Dim xdoc As New XDocument(
         New XElement("Submission",
            New XElement("Enquiry",
               New XElement("customerurn", dms.data("Enquiry", 3)),
               New XElement("enquiryurn", dms.data("Enquiry", 4)),
               New XElement("salestype", sType),
               New XElement("ispartofmulitpurchase", "N"),
               New XElement("contactsource", "1"),
               New XElement("contactmethod", Cmethod),
               New XElement("mediasource", "OTH"),
               New XElement("ModelOfInterest",
                  New XElement("Vehicle",
                     New XElement("isnewvehicle", newUsed),
                     New XElement("description", dms.data("Enquiry", 10) & " " & dms.data("Enquiry", 11) & " " & dms.data("Enquiry", 16)),
                     New XElement("manu", dms.data("Enquiry", 10)),
                     New XElement("model", model),
                     New XElement("isavailable", "1")
                     )
                  ),
                  New XElement("disabled", "0"),
                  New XElement("status", status),
                  New XElement("haspx", hasPx),
                  New XElement("Statistics",
                     New XElement("updated", CurrentTimeStampAdf()),
                     New XElement("updatedby", getNRCAStaffNo(staffNo))
                     )
                  )
               )
            )

工作正常,

但是我现在在运行时需要添加另一个元素,如果存在部分车辆,

问题是当我使用我在C#.NET 4.5项目中使用的代码时

xDoc.Descendants("Enquiry").Last().Add

扩展方法(.Last,.firstordefault)等不存在,这是正确的,它们是特定于.NET 4.5以后还是我错过了什么?

如果这些方法是.NET 4.5 +只有我可以使用的替代方法吗?

1 个答案:

答案 0 :(得分:0)

确保包括:

function drawESGraph() {
  d3.selectAll('.ES__graph__container svg')
    .remove();

  d3.selectAll('.ES__buttons button')
    .remove();

  var $container = $('.ES__graph__container');

  var width = $container.width() / 3;

  var m = 40,
      r = width / 3,
      labelr = r + 20;

  var arc = d3.svg.arc()
              .outerRadius(r)
              .innerRadius(r / 2);

  var pie = d3.layout.pie()
              .value(function(d) {
                return +d.val;
              })
             .sort(null);


  var allBrands = d3.set(data.map(function(d) {
                      return d.brand;
                    })).values();

  var buttons = d3.select('.ES__buttons')
                  .selectAll('button')
                  .data(allBrands)
                  .enter()
                  .append('button')
                  .attr('class', function(d) {
                     return d + ' button';
                  })
                 .text(function(d) {
                   return d;
                  })
                 .on('click', function(d) {
                   updateChart(d);
                 })
                .style('opacity', 0);

  buttons.transition().duration(1000)
         .style('opacity', 1);

  d3.select('.brand1.button')
    .attr('class', 'brand1 button active');

  function updateChart(brand) {
var brandData = data.filter(function(d) {
  return d.brand === brand;
});

var brandDataByYear = d3.nest()
  .key(function(d) {
    return d.year;
  })
  .entries(brandData);

var svg = d3.select('.ES__graph__container')
  .selectAll('svg')
  .data(brandDataByYear)
  .enter()
  .append('svg')
  .style('margin-top', '25px')
  .attr('width', (r + m) * 2)
  .attr('height', (r + m) * 2)
  .attr('id', function(d, i) {
    return 'pie' + i;
  })
  .append('svg:g')
  .attr('transform', 'translate(' + (r + m) + ',' + (r + m) + ')');

var pieLabel = svg.append('svg:text')
  .attr('dy', '.35em')
  .attr('text-anchor', 'middle')
  .text(function(d) {
    return d.key;
  })
  .style('fill', 'black')
  .style('opacity', 0);

pieLabel.transition().duration(1000)
  .style('opacity', 1);

var slice = svg.selectAll('.arc')
  .data(function(d) {
    return pie(d.values);
  })
  .enter()
  .append('g')
  .attr('class', 'arc');

var path = slice.append('svg:path')
  .attr('d', arc)
  .attr('class', function(d) {
    return 'arc ' + d.data.platform;
  })
  .each(function(d) {
    this._current = d;
  });

var text = slice.append('text')
  .text(function(d) {
    if (d.data.val > 0) {
      return d.data.val + '%';
    }
  })
  .attr('transform', function(d) {
    if (d.data.val > 3) {
      return 'translate(' + arc.centroid(d) + ')';
    } else {
      var c = arc.centroid(d),
        x = c[0],
        y = c[1],
        h = Math.sqrt(x * x + y * y);

      return 'translate(' + (x / h * labelr) + ',' + (y / h * labelr) + ')';
    }
  })
  .attr('text-anchor', function(d) {
    if (d.data.val < 3) {
      return (d.endAngle + d.startAngle) / 2 > Math.PI ? 'end' : 'start';
    }
  })
  .attr('dx', function(d) {
    return d.data.val > 3 ? -15 : 18;
  })
  .attr('dy', function(d) {
    return d.data.val > 3 ? 5 : 3;
  })
  .style('fill', function(d) {
    return d.data.val > 3 ? 'white' : 'black';
  })
  .attr('class', 'label');

change();

function change() {
  var newdata = brandDataByYear;

  for (x in newdata) {
    var nslice = d3.select('#pie' + x)
      .data(newdata);

    var npath = nslice.selectAll('path')
      .data(function(d) {
        return pie(d.values);
      })
      .attr('class', function(d) {
        return 'arc ' + d.data.platform;
      });

    npath.transition().duration(1000)
      .attrTween('d', arcTween);

    npath.exit()
      .remove();

    var ntext = nslice.selectAll('.label')
      .data(function(d) {
         return d.values;
       })
      .style('opacity', 0);

    ntext.transition().duration(1000)
      .style('opacity', 1)
      .text(function(d) {
        if (d.val > 0) {
          return d.val + '%';
        }
      })
      // .attr("transform", function(d) {
      //   return "translate(" + 
      //     ( (radius - 12) * Math.sin( ((d.endAngle - d.startAngle) / 2) + d.startAngle ) ) +
      //     ", " +
      //     ( -1 * (radius - 12) * Math.cos( ((d.endAngle - d.startAngle) / 2) + d.startAngle ) ) +
      //   ")";
      //  })
      // .style("text-anchor", function(d) {
      //    var rads = ((d.endAngle - d.startAngle) / 2) + d.startAngle;
      //    if ( (rads > 7 * Math.PI / 4 && rads < Math.PI / 4) || (rads > 3 * Math.PI / 4 && rads < 5 * Math.PI / 4) ) {
      //      return "middle";
      //    } else if (rads >= Math.PI / 4 && rads <= 3 * Math.PI / 4) {
      //      return "start";
      //    } else if (rads >= 5 * Math.PI / 4 && rads <= 7 * Math.PI / 4) {
      //      return "end";
      //    } else {
      //      return "middle";
      //    }
      //  })

    // ntext.exit()
    //      .remove();
  }
}

function arcTween(a) {
  var i = d3.interpolate(this._current, a);

  this._current = i(0);

  return function(t) {
    return arc(i(t));
  }
}
  }

  updateChart('brand1');
}

drawESGraph();

此外,如果您尝试获取特定元素,则可以使用:

Import System.Linq