简体   繁体   中英

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

I am using the xDocument class to create an xml document to send to a webservice,

in the VB program i have this code:

 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

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?

if these methods are .NET 4.5 + only is there an alternative i can use?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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