我目前正在實習,我必須檢查是否為我的專案創建了必要的 XML 檔案,如果它不存在,我必須創建并用節點填充它,但是它不起作用。我可以檢查檔案是否存在,如果不存在,我可以創建它,僅此而已。我試圖添加宣告,但這也不起作用。它只是創建檔案,里面沒有任何東西。這是我為此撰寫的代碼:
If Not (File.Exists(filePathTest)) Then
Using fsCreate As FileStream = File.Create(filePathTest)
Dim xmldocCreate As New XmlDataDocument()
Dim xmldecl As XmlDeclaration
xmldecl = xmldocCreate.CreateXmlDeclaration("1.0", Nothing, Nothing)
xmldocCreate.AppendChild(xmldecl)
End Using
Else
以及 xml 檔案的外觀:
<?xml version="1.0" encoding="utf-8"?>
<Ampoules>
<ampoule>
<nom>product name</nom>
<type>product type</type>
<sousType>product sub-type</sousType>
<code>product reference</code>
<xRay>BW524/99/Ro</xRay>
<quantity>product quantity</quantity>
</ampoule>
<Date>
<date>23/06/2022</date>
</Date>
</Ampoules>
有 57<ampoule></ampoule>個這樣的其他節點,我不能把它們全部放完,所以如果有人也有這個問題的解決方案會很好,盡管我可能知道如何做到這一點。我還必須創建<Date></Date>具有<date></date>內部節點和當前日期作為內部文本的節點,我也不知道如何執行此操作。
我提前感謝您的回答。
uj5u.com熱心網友回復:
您需要閱讀此XML Literals和此XElement才能理解這一點。它創造了我認為你想要的東西,57 個安瓿節點。XElement 和嵌入式查詢是一項強大的功能。
Dim path As String
path = "path to xml file"
Dim xe As XElement
xe = <Ampoules>
<%= From z In Enumerable.Range(1, 57)
Select <ampoule id=<%= z %>>
<nom></nom>
<type></type>
<sousType></sousType>
<code></code>
<xRay></xRay>
<quantity></quantity>
</ampoule>
%>
<Date>
<date><%= DateTime.Now.ToString("dd/MM/yyyy") %></date>
</Date>
</Ampoules>
xe.Save(path)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494799.html
