簡體   English   中英

使用特定的內部文本 vb.net 將子節點添加到父 xml

[英]add child node to parent xml with specfic innertext vb.net

我可以將具有增量 ID 的新子“CharacterDevelopmentID”作為內部文本添加到“CharacterDevelpments” - 沒問題。 這讓我:

  <S2SProject>
   <CharacterDevelopments>
    <CharacterDevelopmentID>1</CharacterDevelopmentID>
    <CharacterDevelopmentID>2</CharacterDevelopmentID>
  </CharacterDevelopments>
 </S2SProject>

添加 CharacterDevelopmentID 后,我需要添加一個額外的子“CharacterDevelopmentName”,但僅限於剛剛創建的 CharacterDevelopmentID,因此新的 XML 應該如下所示:

<S2SProject>
 <CharacterDevelopments>
  <CharacterDevelopmentID>1
   <CharacterDevelopmentName>another test</CharacterDevelopmentName>
  </CharacterDevelopmentID>
  <CharacterDevelopmentID>2
   <CharacterDevelopmentName>yet another test</CharacterDevelopmentName>
  </CharacterDevelopmentID>
 </CharacterDevelopments>
</S2SProject>

我不知道如何以編程方式 select 節點。 每當我嘗試 select 基於 ID nr 的節點時,什么都沒有發生

        fullPath = TSProjectProjectLocation.Text & "\" & TSProjectProjectName.Text

        xmlDoc.Load(fullPath)
        Dim elemList As XmlNodeList = xmlDoc.GetElementsByTagName("CharacterDevelopmentID")
        Dim i As Integer = elemList.Count + 1

        newChild = CType(xmlDoc.CreateNode(Xml.XmlNodeType.Element, "CharacterDevelopment", ""), XmlElement)
        newChild.SetAttribute("CharacterDevelopmentID", i)
        childnode = xmlDoc.CreateElement("CharacterDevelopmentID", i)

        Dim elem1 As XmlElement
        elem1 = xmlDoc.CreateElement("CharacterDevelopmentID")
        elem1.InnerText = i
        parNode = xmlDoc.SelectSingleNode("/S2SProject/CharacterDevelopments")
        parNode.AppendChild(elem1)
        xmlDoc.Save(fullPath) ' --- up to here it works!

        elem1 = xmlDoc.CreateElement("CharacterDevelopmentName")
        elem1.InnerText = NewCharacterDevelopmentName
        parNode = xmlDoc.SelectSingleNode("/S2SProject/CharacterDevelopments/CharacterDevelopmentID[last()]/]") '-- this gives an error XpathException
        parNode.AppendChild(elem1)
        xmlDoc.Save(fullPath)

我已經嘗試了很多方法來讓程序 select 正確的節點,但無濟於事。 誰能指出我正確的方向? 任何能幫助我解決這個問題的東西都將不勝感激!

我正准備離開工作,但我會留下一些代碼讓你思考。

    Dim xe As XElement
    ' for production use
    '  xe = XElement.Load("path here")
    'for testing use literal
    xe = <S2SProject>
             <CharacterDevelopments>
             </CharacterDevelopments>
         </S2SProject>

    'add some CharacterDevelopmentID
    For x As Integer = 1 To 9
        xe.<CharacterDevelopments>.FirstOrDefault.Add(<CharacterDevelopmentID><%= x %></CharacterDevelopmentID>)
    Next

    'select a particular CharacterDevelopmentID
    Dim sel As IEnumerable(Of XElement)
    sel = From el In xe.<CharacterDevelopments>.<CharacterDevelopmentID>
          Where el.FirstNode.ToString = "7"
           Select el Take 1

    If sel.Count = 1 Then
        sel(0).LastNode.AddAfterSelf(<CharacterDevelopmentName>another test</CharacterDevelopmentName>)
    End If

暫無
暫無

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

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