Log in

View Full Version : XML AppendChild returns a system Null Ref ??


Mike Temporale
08-30-2003, 02:11 AM
I'm having some problems saving changes to my XML documents under VB & .Net CF. This code has always worked for me before I ported it to the CF. I think the only change I made was switching SelectSingleNode to GetElementsByTagName (because the select is not recognized under CF)

I have a basic XML doc like the following:
<root>
<lists>
<list name="1" />
</lists>
</root>

and I am looking to append a child under the 'lists' element. Here's the code I'm using:
dim oDom as xmlDocument = new xmldocument
dim oDomList as xmlNode = oDom.CreateElement("list")
dim oDomListAttr as xmlAttribute = oDom.CreateAttribute("name")

oDomList.Attributes.Append(oDomListName)
oDomList.Attributes.GetNamedItem("name").value = "two"

Dim xmlMain as new xmlDocument
xmlMain.load("filename.xml")

dim newSectionnode as xmlNode = xmlMain.ImportNode(oDomList, True)
xmlMain.GetElementsByTagName("//lists").item(0).AppendChild(newSectionNode)

xmlMain.Save("filename.xml")


On the second last line (xmlMain.GetElements...) I get a system.NullReferenceException. Any help you could offer would be great.

-br

smashcasi
08-30-2003, 06:25 PM
I've never messed around with XML in VB, but at first glance it looks like this is failing because GetElementsByTagName() is returning null, most likely because you have no tags named "//lists". Try just plain old "lists".

Mike Temporale
09-02-2003, 01:42 AM
In XML "//" before a element name means get the first element by this name. So if my XML looked like:


<root>
<lists type="tree">
<list name="1" />
</lists>
<lists type="car">
<list name="vw" />
</lists>
</root>


It should find the first element named "lists", the one with a attribute of type tree.

HOWEVER. I thought I would try your suggestion, as I have nothing to loose, and I'm at a stand still. And as it turns out, it worked. The reason is because the version of XML used in .NET CF is an older implementation. I think it's based off the version shipped with IE4. That version of XML does not support the "//" tag. Go figure. :|

Anyway, all is now working. Thanks for the suggestion. Sometimes you just need another set of eyes, and a new (or old) way of looking at things. :wink: