what's up? reading xml

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

I'm doing something wrong in the reading of this file. I think the rest
will work but it keeps telling me something else is using the file.
Nothing is. Any ideas?

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim doc As Xml.XmlDocument = New Xml.XmlDocument
Dim reader As New Xml.XmlTextReader("c:\test.xml")
Dim writer As New Xml.XmlTextWriter("c:\test.xml", Nothing)
Try
doc.LoadXml(reader.Read)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Dim account_number As Xml.XmlNodeList =
doc.GetElementsByTagName("account_number")
Dim reply_code As Xml.XmlNodeList =
doc.GetElementsByTagName("reply_code")
Dim sequence_number As Xml.XmlNodeList =
doc.GetElementsByTagName("sequence_number")

MessageBox.Show(account_number(0).InnerText)
MessageBox.Show(reply_code(0).InnerText)
MessageBox.Show(sequence_number(0).InnerText)

writer.Formatting = Xml.Formatting.Indented
doc.Save(writer)
End Sub
 
cj said:
I'm doing something wrong in the reading of this file. I think the rest
will work but it keeps telling me something else is using the file.
Nothing is. Any ideas?

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim doc As Xml.XmlDocument = New Xml.XmlDocument
Dim reader As New Xml.XmlTextReader("c:\test.xml")
Dim writer As New Xml.XmlTextWriter("c:\test.xml", Nothing)
Try
doc.LoadXml(reader.Read)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Dim account_number As Xml.XmlNodeList =
doc.GetElementsByTagName("account_number")
Dim reply_code As Xml.XmlNodeList =
doc.GetElementsByTagName("reply_code")
Dim sequence_number As Xml.XmlNodeList =
doc.GetElementsByTagName("sequence_number")

MessageBox.Show(account_number(0).InnerText)
MessageBox.Show(reply_code(0).InnerText)
MessageBox.Show(sequence_number(0).InnerText)

writer.Formatting = Xml.Formatting.Indented
doc.Save(writer)
End Sub

Yes, you are using the file.. You have reader opening the file.

Dim reader As New Xml.XmlTextReader("c:\test.xml")
Dim writer As New Xml.XmlTextWriter("c:\test.xml", Nothing)

You will need to close "reader" before you can save.

Chris
 
Thanks for the reply. Apparently instigating the writer right behind
the reader is not good. When I moved the Dim writer down to just above
writer.formatting..... it no longer complained that the file was open on
the doc.loadxml.... line instead it now tells me "The root element is
missing". Sounds like my sample XML file is not formatted correctly.
I'm looking into it. I just grabbed one that was floating around here
to test with.
 
Now I'm getting "The data at the root level is invalid. Line 1, position
1." when I go to read the file doc.LoadXml(reader.Read)
Any help is appreciated. Here's the data:

<?xml version="1.0" encoding="UTF-8"?>
<request>
<sequence_number>000002</sequence_number>
<account_number>103363463355</account_number>
<response_fields>
<reply_code>201</reply_code>
</response_fields>
</request>

I've tried it w/o the <?xml version="1.0" encoding="UTF-8"?> line but
same thing.

Code now reads:

Dim doc As Xml.XmlDocument = New Xml.XmlDocument
Dim reader As New Xml.XmlTextReader("c:\test.xml")

Try
doc.LoadXml(reader.Read)
Catch ex As Exception
 
Now I'm getting "The data at the root level is invalid. Line 1, position
1." when I go to read the file doc.LoadXml(reader.Read)
Any help is appreciated. Here's the data:

<?xml version="1.0" encoding="UTF-8"?>
<request>
<sequence_number>000002</sequence_number>
<account_number>103363463355</account_number>
<response_fields>
<reply_code>201</reply_code>
</response_fields>
</request>

I've tried it w/o the <?xml version="1.0" encoding="UTF-8"?> line but
same thing.

Code now reads:

Dim doc As Xml.XmlDocument = New Xml.XmlDocument
Dim reader As New Xml.XmlTextReader("c:\test.xml")

Try
doc.LoadXml(reader.Read)
Catch ex As Exception
.
.
.
.


I don't know much about XML (yet), but had reason the other day to use
a reader in an app. If you are using Net 2.0, this example would read
the above file. It assumes the contens are all text, but you could
just as well read various other content such as integers
(ReadElementContentAsInteger) etc.

Dim settings As New XmlReaderSettings()
'no special settings used in this example
Dim reader As XmlReader = XmlReader.Create("c:\Test.xml",
settings)
With reader
.Read() '<request>
var1 = .ReadElementString 'sequence_number
var2 = .ReadElementString 'account_number
.ReadStartElement("response_fields")
var3 = .ReadElementString 'reply_code
.ReadEndElement()

.Close()
End With

There is a companion XMLWriter.Create method which can be found in the
VB2005 Help file.

Gene
 
Hi gene,

I guess I need to start including that I'm using VB2003. It doesn't
appear to be available in VB2003.
 
Solution:

First, off there was no need for me to have used reader.
Second, I meant to use doc.load not doc.loadxml.

The code to read the file is:

Dim doc As Xml.XmlDocument = New Xml.XmlDocument
Try
doc.Load("c:\test.xml")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Thanks to those who tried to help. Just thought I'd share the solution.
 
Back
Top