在运行时向 xdocument 添加元素 vb.net
Adding an element to an xdocument at runtime vb.net
我有两个程序,
一个是用 C# .NET V4.5 编写的 Web 服务,另一个是用 VB.NET V4.0
编写的
我正在使用 xDocument class 创建一个 xml 文档以发送到网络服务,
在 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 + 只有我可以使用的替代方法吗?
确保您包括:
Import System.Linq
此外,如果您尝试获取特定元素,您可以使用:
var q= from c in xmlFile.Descendants("Enquiry") select c;
我有两个程序,
一个是用 C# .NET V4.5 编写的 Web 服务,另一个是用 VB.NET V4.0
编写的我正在使用 xDocument class 创建一个 xml 文档以发送到网络服务,
在 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 + 只有我可以使用的替代方法吗?
确保您包括:
Import System.Linq
此外,如果您尝试获取特定元素,您可以使用:
var q= from c in xmlFile.Descendants("Enquiry") select c;