Dynamic Visio – XML & VBA (Part 1)

What ever you think Visio is – there’s a lot-more to it than flow-charts and block-diagrams. Thanks to the fact that it is part of Microsoft’s Office Suite – it has VBA (Visual Basic for Applications) built in and that ‘comes with’ the functionality to process XML (Extensible Mark-up Language) files. Simply put – Visio can be used to dynamically create illustrations by reading an input ‘specification’ defined in an XML file (that could be an output of another process).

The XML

Consider this simple XML file defining three ‘items’ with positional and dimensional values :

<items>
  <item>
    <name>Item One</name>
    <left>1</left>
    <bottom>1</bottom>
    <width>2</width>
    <height>0.5</height>
  </item>
  <item>
    <name>Item Two</name>
    <left>4</left>
    <bottom>1</bottom>
    <width>2</width>
    <height>0.5</height>
  </item>
  <item>
    <name>Item Three</name>
    <left>1</left>
    <bottom>2</bottom>
    <width>2</width>
    <height>0.5</height>
  </item>
</items> 

The VBA

The method named : Document_DocumentOpened() will execute automatically when the Visio document (to which it is ‘attached’) is opened. It will open the local XML file named : Items-01.xml, cycle through each ‘Item’ in ‘Items’ calling the method named Create_Shape() passing in the positional and dimensional values of each ‘Item’.

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
  
    Dim XDoc As Object
    Set XDoc = CreateObject("MSXML2.DOMDocument")
    XDoc.async = False: XDoc.validateOnParse = False
    XDoc.Load ("C:\Users\Joe\Documents\Work\Items-01.xml")
    Set Items = XDoc.documentElement
    
    Dim Name As String
    Dim Left As Double
    Dim Bottom As Double
    Dim Width As Double
    Dim Height As Double
    
    For Each Item In Items.childNodes
      
      Name = Item.childNodes(0).Text
      Left = Item.childNodes(1).Text
      Bottom = Item.childNodes(2).Text
      Width = Item.childNodes(3).Text
      Height = Item.childNodes(4).Text
    
      Call Create_Shape(Left, Bottom, Left + Width, Bottom + Height, Name)
    Next Item
    
    Set XDoc = Nothing
End Sub
Sub Create_Shape(xStart As Double, yStart As Double, xEnd As Double, yEnd As Double, Name As String)

  Dim vsoShape As Visio.Shape

  Set vsoShape = ActivePage.DrawRectangle(xStart, yStart, xEnd, yEnd)
  vsoShape.Text = Name

End Sub

The Visio

When each ‘Item’ is processed – the following Visio page with three rectangles will have been created :

Advertisements

Saving the text of named VB.NET controls to XML

How to save the text of named VB.NET controls to an XML file

VB.NET

Protected Sub SaveXML_2()

  Dim settings As New XmlWriterSettings()
  settings.Indent = True

  Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\SaveXML_002.xml", settings)

  With XmlWrt
    
    .WriteStartDocument()
    .WriteStartElement("Cells")

      .WriteStartElement("customercontact")
      .WriteString(customercontact.Text)
      .WriteEndElement()

      .WriteStartElement("contacttitle")
      .WriteString(contacttitle.Text)
      .WriteEndElement()

    .WriteEndElement()
    .WriteEndDocument()
    .Close()

  End With
End Sub

XML (output)

<Cells>
  <customercontact>Adam Watson</customercontact>
  <contacttitle>Scientist</contacttitle>
</Cells>

Reading XML Nodes into correspondingly named VB.NET Textboxes

Question : How do I read XML node-values (from an XML file) into correspondingly named VB.NET Textbox controls ?

.xml



    123456789
    987654321
    Programming Widgets Inc.
    Ben Franklin
    Lead Programmer
    ben.franklin@pwi.com
   

.aspx.vb

Protected Sub Page_Load() Handles Me.Load

    Dim settings As New XmlReaderSettings()
    settings.ConformanceLevel = ConformanceLevel.Fragment
    settings.IgnoreWhitespace = True
    settings.IgnoreComments = True

    Dim XML_Reader As XmlReader = XmlReader.Create(C:\XML\basic-information.xml, settings)

    XML_Reader.MoveToContent()

    While XML_Reader.Read()

      If XML_Reader.NodeType = XmlNodeType.Element Then

        Dim NodeName As String = XML_Reader.Name

        XML_Reader.Read()
        Dim NodeValue As String = XML_Reader.Value

        Dim PageControl As RadTextBox = RadMultiPage1.FindControl(NodeName)

        If (Not PageControl Is Nothing) Then
          PageControl.Text = NodeValue
        Else
          ' "Control not found" - error message
        End If

      End If

      End While
      XML_Reader.Close()

End Sub