简体   繁体   English

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

[英]Adding an element to an xdocument at runtime vb.net

I have two programs, 我有两个程序

one is a webservice written in C# .NET V4.5 and the other is written in VB.NET V4.0 一个是用C#.NET V4.5编写的Web服务,另一个是用VB.NET V4.0编写的

I am using the xDocument class to create an xml document to send to a webservice, 我正在使用xDocument类创建要发送到Web服务的xml文档,

in the VB program i have this code: 在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))
                     )
                  )
               )
            )

which works fine, 效果很好,

however i now at runtime need to add another element if there is a part ex vehicle present, 但是,如果有零件存在,我现在在运行时需要添加另一个元素,

The problem is when i use the code which i use in the C# .NET 4.5 project 问题是当我使用在C#.NET 4.5项目中使用的代码时

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

The extension methods (.Last, .firstordefault) etc do not exist, is this correct and they are specific to .NET 4.5 onwards or am i missing something? 扩展方法(.Last,.firstordefault)等不存在,这是正确的吗,它们特定于.NET 4.5或更高版本?

if these methods are .NET 4.5 + only is there an alternative i can use? 如果这些方法是.NET 4.5 +,那么我可以使用替代方法吗?

Make sure you included: 确保您包括:

Import System.Linq

Also if you try to get a specific element, you can use: 另外,如果您尝试获取特定元素,则可以使用:

var q= from c in xmlFile.Descendants("Enquiry") select c;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM