|
-
Jul 22nd, 2009, 05:18 PM
#1
Thread Starter
Addicted Member
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
-
Jul 22nd, 2009, 05:42 PM
#2
Re: Not reading all XML file...
it is reading twice. the first set of values appear to be being overwritten by the second set.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 22nd, 2009, 05:50 PM
#3
Thread Starter
Addicted Member
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
-
Jul 22nd, 2009, 05:55 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 23rd, 2009, 01:59 PM
#5
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)
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|