Question

I'm trying to figure out how to select the last child of the currently selected xml node. I'm having a bit of trouble.

the xml document starts out like this

<project>
   <book>
    ...
   </book>
   <characters>
     <character>
      .....
     </character>
   </characters>
 </project>

I need to be able to append a new character element inside characters along with child nodes inside with data.

I've used an if else statement for if characters node exists and if not. I'm working on the if it does exist now. Basically the document will already contain one character.

Here's the code I'm working with right now.

nav = nav.SelectSingleNode("/project/characters")
        nav.AppendChildElement("", "character", "", "")
        nav.SelectSingleNode("//project/characters/*[last()]")
        nav.AppendChildElement("", "name", "", characterName)
        nav.AppendChildElement("", "race", "", characterRace)
        nav.AppendChildElement("", "age", "", characterAge)
        nav.AppendChildElement("", "name", "", characterName)
        nav.AppendChildElement("", "race", "", characterRace)
        nav.AppendChildElement("", "age", "", characterAge)
        nav.AppendChildElement("", "gender", "", characterGender)
        nav.AppendChildElement("", "origin", "", characterOrigin)
        nav.AppendChildElement("", "eye", "", characterEyeColor)
        nav.AppendChildElement("", "hair", "", characterHairColor)
        nav.AppendChildElement("", "weight", "", characterWeight)
        nav.AppendChildElement("", "height", "", characterHeight)
        nav.AppendChildElement("", "occupation", "", characterOccupation)
        nav.AppendChildElement("", "birthmarks", "", characterBirthmarks)
        nav.AppendChildElement("", "piercings", "", characterPiercings)
        nav.AppendChildElement("", "tattoos", "", characterTattoos)
        nav.AppendChildElement("", "scars", "", characterScars)

        Do While counter < traitCount

            nav.AppendChildElement("", "trait", "", characterTraits.AsReadOnly(counter))

            counter = counter + 1
        Loop

        counter = 0

        Do While counter < habitCount

            nav.AppendChildElement("", "habit", "", characterHabits.AsReadOnly(counter))

            counter = counter + 1
        Loop

        nav.AppendChildElement("", "bio", "", characterBio)


        document.Save(ProjectDataMod.projectPathMod + ProjectDataMod.projectNameMod + ".xml")

Any help would be greatly appreciated. Thanks for your time.

Jb.

Was it helpful?

Solution

Edited

You could use XPath for this. If I understand right you want the last child of the "characters" node:

nav.SelectSingleNode( "//project/characters/*[last()]" )

If you have several characters nodes you could do:

nav.SelectSingleNode( "//project/characters[last()]/*[last()]" )

To get the last child of the last characters node.

OTHER TIPS

You might consider using xpath to query your xml. It has a built in function to find the last child of a node.

http://support.microsoft.com/kb/301220

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top