簡體   English   中英

在運行時vb.net將元素添加到xdocument

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

我有兩個程序

一個是用C#.NET V4.5編寫的Web服務,另一個是用VB.NET V4.0編寫的

我正在使用xDocument類創建要發送到Web服務的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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM