Not reading all XML file...
I have an XML file, shown below:
Code:
<?xml version="1.0" standalone="no"?>
<!--RISInsight server settings-->
<Configuration>
<Server>
<ConnectionMode>ServerName</ConnectionMode>
<ServerName>(local)</ServerName>
<IPAddress>NR</IPAddress>
<IPPort>NR</IPPort>
<SMTPServer></SMTPServer>
<SMTPPort></SMTPPort>
<SMTPUserName></SMTPUserName>
<SMTPPassword></SMTPPassword>
<LicenseFile>C:\Program Files\Ticodi\ReviewInsight\License\ReviewInsight.lic</LicenseFile>
</Server>
<Server>
<ConnectionMode>ServerName</ConnectionMode>
<ServerName>(local)</ServerName>
<IPAddress>NR</IPAddress>
<IPPort>NR</IPPort>
<SMTPServer></SMTPServer>
<SMTPPort></SMTPPort>
<SMTPUserName></SMTPUserName>
<SMTPPassword></SMTPPassword>
<LicenseFile>C:\Program Files\Ticodi\ReviewInsight\License\ReviewInsight.lic</LicenseFile>
</Server>
</Configuration>
The problem is that I want to read the complete XML file, however because I am repeating the same XMLElementName it only seems to read the first element, the code I am using is the following:
Code:
Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(Application.StartupPath & "\settings.ini")
Do While (reader.Read())
Dim addToList As Boolean = False
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
xmlElementName = reader.Name
Case Xml.XmlNodeType.Text
If xmlElementName = "ConnectionMode" Then
RISData.Instance.ConnectionMode = reader.Value
ElseIf xmlElementName = "IPAddress" Then
RISData.Instance.IPAddress = reader.Value
ElseIf xmlElementName = "IPPort" Then
RISData.Instance.IPPort = reader.Value
addToList = True
ElseIf xmlElementName = "ServerName" Then
RISData.Instance.ServerName = reader.Value
ElseIf xmlElementName = "LicenseFile" Then
RISData.Instance.LicenseLocation = reader.Value
ElseIf xmlElementName = "SMTPServer" Then
RISData.Instance.SMTPServer = reader.Value
ElseIf xmlElementName = "SMTPPort" Then
RISData.Instance.SMTPPort = reader.Value
ElseIf xmlElementName = "SMTPUserName" Then
RISData.Instance.SMTPUserName = reader.Value
ElseIf xmlElementName = "SMTPPassword" Then
RISData.Instance.SMTPPassword = reader.Value
End If
If addToList = True Then
Me.lstServer.BeginInit()
If RISData.Instance.ConnectionMode = "ServerName" Then
Dim data As Object() = New Object() {RISData.Instance.ServerName, "N/A"}
Me.lstServer.Rows.Items.Add(New Node(data))
Else
Dim data As Object() = New Object() {RISData.Instance.IPAddress, RISData.Instance.IPPort}
Me.lstServer.Rows.Items.Add(New Node(data))
End If
Me.lstServer.EndInit()
addToList = False
End If
End Select
Loop
reader.Close()
Is there a way that I can make it continue to read the XML file?
Thanks in advance
Simon
Re: Not reading all XML file...
it is reading twice. the first set of values appear to be being overwritten by the second set.
Re: Not reading all XML file...
It is only reading once, if I add the msgbox in the following code, the msgbox should appear twice however it only appears once
Code:
If xmlElementName = "ConnectionMode" Then
RISData.Instance.ConnectionMode = reader.Value
MsgBox("hi")
ElseIf xmlElementName = "IPAddress" Then
Re: Not reading all XML file...
ok. put a in after
vb Code:
Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(Application.StartupPath & "\settings.ini")
then step through the code (F10) + see where its crashing out
Re: Not reading all XML file...
You can use XML serialization to populate a class. I like to use visual studio's xsd.exe utility to get the schema and serializable class:
Create schema from XML
Code:
C:\Program Files\Microsoft Visual Studio 9.0\VC>xsd C:\temp\RISInsight.xml /outputdir:c:\temp
Create class from schema
Code:
C:\Program Files\Microsoft Visual Studio 9.0\VC>xsd C:\temp\RISInsight.xsd /classes /language:vb /outputdir:c:\temp
This creates RISInsight.vb, which you can add to a project. In order to populate this class create a deserialization method:
Code:
Private Function DeserializeFromString(ByVal input As String) As Configuration
'Convert to byte array
Dim encoding As New System.Text.UnicodeEncoding
Dim bytes() As Byte = encoding.GetBytes(input)
'read bytes into stream
Dim ms As New System.IO.MemoryStream
ms.Write(bytes, 0, bytes.Length)
ms.Position = 0
'get object from bytes
Dim xs As New Xml.Serialization.XmlSerializer(GetType(Configuration))
Return DirectCast(xs.Deserialize(ms), Configuration)
End Function
then call it
Code:
'get data from file
Dim xmlData = IO.File.ReadAllText("C:\temp\RISInsight.xml")
'create object from data
Dim o = DeserializeFromString(xmlData)
Console.WriteLine(o.Items.Count)