Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append to an XML document

Status
Not open for further replies.

tembalena

Programmer
Apr 17, 2001
37
ZA
I have an xml document that I need to keep appending data to, using asp.net and ado.net. Please would someone tell me the best way to do this.

For example, the XML looks like this:
<XMLOutput>
<Contacts>
<ID>1</ID>
<ContactName>Greg</ContactName>
<Details>Some details here.</Details>
</Contacts>
</XMLOutput>

And from data captured, I need to add Contacts (containing ID, ContactName, Details) to this file. How do I do that using asp.net and ado.net? I presume I have to load the document each time and append to the end of the file, but I have no idea how to create the new nodes/elements and add them.

Thanks so much for any help offered.
 
I just had to do this exact thing but I used regular ASP and to my beginner's knowledge you do not need ADO to manipulate an XML document. Are you attempting to use ASP.Net in order to use its datagrid?

Because I'm at work right now I can't respond in the detail you need - but I have a great, simple solution (I'm an XML beginner, too).

I'll post another message after 4:30 today (4/11/03).

Cheers,
cyclegeek
 
To perform simple XML file modifications as you describe, the .NET library contains XML DOM classes that you will use to read in the existing file. Then you will use objects (DOM Objects) to create new elements and append them as children to the correct parent elements to maintain your XML element relationships. Then you write the changes back out to the file overwriting what was previously there.

Now you mention using ASP.NET. This suggests a multi-user environment which will raise a contention issue regarding writing changes to a single shared file. If in fact you need multi-user capability your project design may need to consider alternative data management solutions.

-pete
 
Thanks for all the help. This is what I've done and it's working:

Dim m_OutFileName As String = Server.MapPath(&quot;Output.xml&quot;)
Dim ok As Boolean = True
Dim xmlDoc As New XmlDocument()

Try

'Load the file.
xmlDoc.Load(m_OutFileName)

'Create a new &quot;Advertisers&quot; element.
Dim elAdvertisers As XmlElement = xmlDoc.CreateElement(&quot;Advertisers&quot;)

'Create &quot;name,&quot; and &quot;details&quot; elements.
Dim elName As XmlElement = xmlDoc.CreateElement(&quot;name&quot;, sName)
Dim elDetails As XmlElement = xmlDoc.CreateElement(&quot;details&quot;, sDetails)

'Add them as children of the &quot;Advertisers&quot; element.
elAdvertisers.AppendChild(elName)
elAdvertisers.AppendChild(elDetails)

'Get a reference to the document's root element.
Dim elRoot As XmlElement = xmlDoc.DocumentElement

'Add the &quot;Advertisers&quot; element as a child of the root.
elRoot.AppendChild(elAdvertisers)

'Save the document.
xmlDoc.Save(m_OutFileName)

Catch e As Exception

ok = False
Response.Write(&quot;Exception: &quot; & e.Message)

Finally

If ok Then
Response.Write(&quot;Element added successfully.&quot;)
Else
Response.Write(&quot;An error occurred.&quot;)
End If

End Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top