'Create XML file, add element to existing file:
Dim strFile = Server.MapPath("YouTube.xml")
If Not File.Exists(strFile) Then
Dim writer As New XmlTextWriter(strFile, System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement("Table")
createNode(txtYouTubeLink.Text, writer)
'createNode(2, "Product 2", "2000", writer)
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
lblYoutubeMessage.Text = "Successfully Created!"
Else
Dim myXmlDocument As XmlDocument = New XmlDocument()
myXmlDocument.Load(strFile)
Dim rootNode As XmlElement = myXmlDocument.DocumentElement("YouTube")
Dim newAppPathElement As XmlElement = myXmlDocument.CreateElement("webLink")
newAppPathElement.InnerXml = txtYouTubeLink.Text
rootNode.AppendChild(newAppPathElement)
myXmlDocument.Save(strFile)
myXmlDocument = Nothing
End If
-- READ XML File to display:
Try
Dim m_xmlr As XmlTextReader
'Create the XML Reader
Dim strFile = Server.MapPath("YouTube.xml")
Dim ThisLink As String = ""
m_xmlr = New XmlTextReader(strFile)
'Disable whitespace so that you don't have to read over whitespaces
m_xmlr.WhitespaceHandling = WhitespaceHandling.None
'read the xml declaration and advance to family tag
m_xmlr.Read() 'read the family tag
m_xmlr.Read() 'Load the Loop
While Not m_xmlr.EOF
'Go to the name tag
m_xmlr.Read()
'if not start element exit while loop
If Not m_xmlr.IsStartElement() Then
Exit While
End If
Dim ColumnAttribute = m_xmlr.GetAttribute("Youtube")
m_xmlr.Read()
ThisLink = m_xmlr.ReadElementString("webLink")
If ThisLink <> "" Then Exit While
End While
'close the reader
m_xmlr.Close()
If ThisLink <> "" Then
litYouTube.Text = GetYouTubeScript(ThisLink)
Else
litYouTube.Text = ""
End If
Catch
End Try
|