XMLTextWriter with XMLTextReader

ManicCW

Well-known member
Joined
Jul 21, 2005
Messages
428
Location
Mostar
Programming Experience
Beginner
Please read and comment!

HI,
I'm building a simple Image Gallery that will store data in XML (it's new for me :eek: ). I will write donw the code that I have for now and you are wellcom to write any suggestions!!!

I think this can be optimized:

First: I need to add new nodes to xml file when image is uploaded. I heard that it's better to use XMLTextReader combined with XMLTextWriter to accomplish this, but I don't know how to do it so I used XMLDocument and load it. This is not good because I belive it loads entire document in memory (there must be a better way). Anyway here is some code:

Sub for writing in XML:

VB.NET:
[SIZE=2][COLOR=#008000]'create XML file if it does not exists
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XMLPath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = FilePath + [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cmbAlbum.SelectedValue + "\Data.xml"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] writer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlTextWriter
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(XMLPath) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'create file with root node
[/COLOR][/SIZE][SIZE=2]writer = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlTextWriter(XMLPath, [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] writer
.Formatting = Formatting.Indented
.Indentation = 2
.WriteStartDocument()
.WriteComment("Images for album: " + [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cmbAlbum.SelectedValue)
.WriteStartElement("Gallery")
.WriteEndElement()
.Flush()
.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'write to file
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] docXML [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument
docXML.Load(XMLPath)
[/SIZE][SIZE=2][COLOR=#008000]'parent node
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] imageNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Image")
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] docXML("Gallery").HasChildNodes [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]imageNode.SetAttribute("ID", GetNewID(docXML, "Image", "ID"))
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]imageNode.SetAttribute("ID", 1)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]docXML.DocumentElement.AppendChild(imageNode)
[/SIZE][SIZE=2][COLOR=#008000]'child nodes
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] nameNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Name")
nameNode.InnerText = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtImageName.Text.Trim.ToLower.Replace(" ", "") + [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cmbImageType.SelectedValue
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dateNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Date")
dateNode.InnerText = [/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2].Now.Date
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] titleNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Title")
titleNode.InnerText = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtTitle.Text.Trim
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] descriptionNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Description")
descriptionNode.InnerText = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtDescription.Text.Trim
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] albumNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlElement = docXML.CreateElement("Album")
albumNode.InnerText = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cmbAlbum.SelectedValue
[/SIZE][SIZE=2][COLOR=#008000]'appending child to parent
[/COLOR][/SIZE][SIZE=2]imageNode.AppendChild(nameNode)
imageNode.AppendChild(dateNode)
imageNode.AppendChild(titleNode)
imageNode.AppendChild(descriptionNode)
[/SIZE][SIZE=2][COLOR=#008000]'save file
[/COLOR][/SIZE][SIZE=2]docXML.Save(XMLPath)
docXML = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE]

I use this funcion to retrive last ID of the image node:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetNewID([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] docXML [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlDocument, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Node [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Attribute [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] LastNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNode
LastNode = docXML.SelectSingleNode("//Gallery/" & Node & "[position()=last()]")
GetNewID = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](LastNode.Attributes(Attribute).Value) + 1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]

And the XML file:

VB.NET:
<?xml version="1.0"?>
<!--Images for album: Music-->
<Gallery>
  <Image ID="1">
    <Name>someimage.jpg</Name>
    <Date>15.11.2005</Date>
    <Title>Some Title</Title>
    <Description>Image description here.</Description>
  </Image>
  <Image ID="2">
    <Name>someimage2.jpg</Name>
    <Date>15.11.2005</Date>
    <Title>Some Title 2</Title>
    <Description>Image description 2 here.</Description>
  </Image>
</Gallery>

PLEASE COMMENT TO OPTIMIZE THIS!!!
 
Last edited:
Back
Top