Chacaman
Chacaman

Reputation: 51

wmi to xml using for each

guys thanks for time and help i have a function for get installed software on the pc i have a list of string for add: Software Name, Software version, Software install Date my problem is in the for each i need to add two second xml tags for example i have software name:

Dim XMLElement As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareName"))

y need more tags for Software version and instalation date in the same for each

Dim XMLElement As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareVersion"))
Dim XMLElement As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareInstallDate"))

how i can do that?

  Try
        Dim strSoftware As New List(Of String)
        Dim regkey, subkey As Microsoft.Win32.RegistryKey
        Dim SoftwareName As String
        Dim SoftwareVer As String
        Dim includes As Boolean
        Dim softwareInstallDate As String
        Dim xDoc As New XmlDocument
        Dim xNode As XmlNode = xDoc.AppendChild(xDoc.CreateElement("ComputerInfo"))
        Dim XMLMasterElement As XmlNode = xNode.AppendChild(xDoc.CreateElement("Software"))
        Dim regpath As String = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
        regkey = My.Computer.Registry.LocalMachine.OpenSubKey(regpath)
        Dim subkeys() As String = regkey.GetSubKeyNames
        For Each subk As String In subkeys
            subkey = regkey.OpenSubKey(subk)
            SoftwareName = subkey.GetValue("DisplayName", "").ToString
            SoftwareVer = subkey.GetValue("DisplayVersion", "").ToString
            softwareInstallDate = subkey.GetValue("InstallDate", "").ToString
            If SoftwareName <> "" Then
                includes = True
                If SoftwareName.IndexOf("Hotfix") <> -1 Then includes = False
                If SoftwareName.IndexOf("Security Update") <> -1 Then includes = False
                If SoftwareName.IndexOf("Update for") <> -1 Then includes = False
                If includes = True Then strSoftware.Add(SoftwareName.ToString)
                strSoftware.Add(SoftwareVer.ToString)
                strSoftware.Add(softwareInstallDate.ToString)
            End If
        Next

        For Each Element As String In strSoftware
            Dim XMLElement As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareName"))
            XMLElement.InnerText = Element.ToString()
        Next

        xNode.AppendChild(xDoc.CreateElement("Software"))
        Dim xWriter As New IO.StringWriter()
        Dim xml_writer As New XmlTextWriter(xWriter)
        xDoc.WriteTo(xml_writer)
        Dim XMSSoftInfo As String = xWriter.ToString()
        Return XMSSoftInfo
    Catch ex As Exception
        WriteEventLogError("error Module GetSoft" + ex.Message)
        Throw New Exception(ex.Message)
    End Try

Upvotes: 0

Views: 93

Answers (1)

har07
har07

Reputation: 89285

Your code will be more readable if you use 3 separate lists to store each type of information : software name, version, and install date (or maybe even better if you create proper model). For example :

Dim softwareName As New List(Of String)
Dim softwareVersion As New List(Of String)
Dim softwareInstallDate As New List(Of String)
......
......
'store each information in proper list variable'
softwareName.Add(SoftwareName)
softwareVersion.Add(SoftwareVer)
softwareInstallDate.Add(softwareInstallDate)
......
......
'add information from above 3 lists to XML'
Dim size = softwareName.Count
For i As Integer = 0 To size - 1
    Dim name As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareName"))
    Dim version As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareVersion"))
    Dim installDate As XmlNode = xNode.AppendChild(xDoc.CreateElement("SoftwareInstallDate"))
    name.InnerText = softwareName(i)
    version.InnerText = softwareVersion(i)
    installDate.InnerText = softwareInstallDate(i)
Next

Upvotes: 1

Related Questions