How to create a XML Document with asp.net 2.0

D

Dikkuuhh

How can i create a XMLDocument??

This is what i have:

Dim objNode As XmlNode

Dim objAttrib As XmlAttribute
objNode = objXML.DocumentElement("Person")

objAttrib = objXML.CreateAttribute("PERSONID")
objAttrib.Value = "p7"
objNode.Attributes.SetNamedItem(objAttrib)
objXML.DocumentElement.AppendChild(objNode)

Then i get an error like this:

System.NullReferenceException: Object reference not set to an instance
of an object.

Can somebody help me out?
 
D

Dikkuuhh

Now this is my code:

Dim objXML As New System.Xml.XmlDocument()
Dim objNode As XmlNode
Dim objAttrib As XmlAttribute
objNode = objXML.DocumentElement("Person")

objAttrib = objXML.CreateAttribute("PERSONID")
objAttrib.Value = "p7"
objNode.Attributes.SetNamedItem(objAttrib)
objXML.DocumentElement.AppendChild(objNode)

But i`m still getting the same error....
 
M

Martin Honnen

Dikkuuhh wrote:

Dim objXML As New System.Xml.XmlDocument()

This line creates the XmlDocument instance but it is empty, you can
either load existing XML into it with the Load method to load from a URL
or stream (or the LoadXml method if you have a string with the XML
markup) or you need to populate the document with nodes by first
creating them and inserting them as needed e.g. (watch out for line
breaks VB does not allow but the news posting might have)

Dim rootElement As XmlElement =
objXML.CreateElement("some-element-name")
Dim someElement as XmlElement =
objXML.CreateElement("another-element-name")
someElement.SetAttribute("attribute-name", "attribute value")
rootElement.AppendChild(someElement)
objXML.AppendChild(rootElement)

objXML.Save("file.xml")

If you still have problems then I suggest microsoft.public.dotnet.xml is
a better place than comp.text.xml.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top