Aggiunta nodo ad un file xml

mercoledì 14 ottobre 2009 - 10.23

Giscotech Profilo | Junior Member

Salve a tutti,

ho l'esigenza di scrivere in un file xml così strutturato :

<galleries>
<gallery filename="gallery1" thumbnail="Living 1_small.jpg" description="Living1" numphoto="6">
<photo filename="photo1" thumbnail="laptop.jpg" description="Laptop" />
<photo filename="photo2" thumbnail="router.jpg" description="Router" />
<photo filename="photo3" thumbnail="screen.jpg" description="Screen" />
<photo filename="photo4" thumbnail="bagno-1.jpg" description="Laptop" />
<photo filename="photo5" thumbnail="conversazione-1.jpg" description="Router" />
<photo filename="photo6" thumbnail="pranzo-1.jpg" description="Screen" />
</gallery>
</galleries>

Con il seguente codice riesco ad aggiungere un elemento gallery:

Dim xmldoc As New XmlDocument()
Dim strOutput As String = ""

xmldoc.Load(Server.MapPath("Spazi Commerciali.xml"))

Dim eleGallery As XmlElement = xmldoc.CreateElement("gallery5")
Dim attStyle1 As XmlAttribute = xmldoc.CreateAttribute("filename")
Dim attStyle2 As XmlAttribute = xmldoc.CreateAttribute("thumbnail")
Dim attStyle3 As XmlAttribute = xmldoc.CreateAttribute("description")
Dim attStyle4 As XmlAttribute = xmldoc.CreateAttribute("numPhoto")
eleGallery.SetAttribute("filename", "gallery5")
eleGallery.SetAttribute("thumbnail", "c:\...")
eleGallery.SetAttribute("description", "nuova galleria")
eleGallery.SetAttribute("numPhoto", "6")

Dim root As XmlElement = xmldoc.Item("galleries")
root.AppendChild(eleGallery)

xmldoc.Save(Server.MapPath("Spazi Commerciali.xml"))

ora dovrei aggiungere un elemento photo
ci ho provato in mille modi ma nn ci riesco.
Sapreste darmi una mano? Grazie.
Giuseppe Scordo
Giscotech Informatics

alx_81 Profilo | Guru

Ciao,
eccoti un esempio:

Dim xmldoc As New XmlDocument() ' galleries Dim xmlGalleriesElement As XmlElement = xmldoc.CreateElement("galleries") xmlGalleriesElement.SetAttribute("filename", "gallery1") xmlGalleriesElement.SetAttribute("thumbnail", "Living 1_small.jpg") xmlGalleriesElement.SetAttribute("description", "Living1") xmlGalleriesElement.SetAttribute("numphoto", "6") ' photos ' creazione Dim xmlPhotoElement As XmlElement = xmldoc.CreateElement("photo") xmlPhotoElement.SetAttribute("filename", "photo1") xmlPhotoElement.SetAttribute("thumbnail", "laptop.jpg") xmlPhotoElement.SetAttribute("description", "Laptop") ' aggiunta xmlGalleriesElement.AppendChild(xmlPhotoElement) ' creazione xmlPhotoElement = Nothing xmlPhotoElement = xmldoc.CreateElement("photo") xmlPhotoElement.SetAttribute("filename", "photo2") xmlPhotoElement.SetAttribute("thumbnail", "router.jpg") xmlPhotoElement.SetAttribute("description", "Router") ' aggiunta xmlGalleriesElement.AppendChild(xmlPhotoElement) ' creazione xmlPhotoElement = Nothing xmlPhotoElement = xmldoc.CreateElement("photo") xmlPhotoElement.SetAttribute("filename", "photo3") xmlPhotoElement.SetAttribute("thumbnail", "screen.jpg") xmlPhotoElement.SetAttribute("description", "Screen") ' aggiunta xmlGalleriesElement.AppendChild(xmlPhotoElement) ' ... ' aggiunta a nodo principale xmldoc.AppendChild(xmlGalleriesElement) xmldoc.Save("C:\a.xml")
--

Alessandro Alpi | SQL Server MVP
MCP|MCITP|MCTS|MCT

http://www.alessandroalpi.net
http://blogs.dotnethell.it/suxstellino
http://mvp.support.microsoft.com/profile/Alessandro.Alpi
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5