" /> " /> "/>

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

209 Views Asked by At

I can add a new child "CharacterDevelopmentID" with an incremental ID as innertext to "Characterdevelpments" - no problem. This gets me:

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

Upon the adding of the CharacterDevelopmentID I need to add an additional child "CharacterDevelopmentName", but only to the CharacterDevelopmentID that was just created so the new XML should look like this:

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

I can't figure out how to select the node programatically. Whenever I try to select the node based on the ID nr nothing happens

        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)

I have tried a number of things to get the program to select the right node, but to no avail. Can anyone point me in the right direction? Anything that helps me figure this out would be most appreciated!

1

There are 1 best solutions below

0
dbasnett On

I'm getting ready to leave work but I'll leave some code to get you thinking.

    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