473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

what's up? reading xml

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
Mar 22 '06 #1
6 1784
cj wrote:
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
Mar 22 '06 #2
cj
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.

cj wrote:
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

Mar 22 '06 #3
cj
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
Mar 22 '06 #4
On Wed, 22 Mar 2006 17:02:01 -0500, cj <cj@nospam.nospam> wrote:
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
.
.
.
.
cj wrote:
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.

cj wrote:
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

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
Mar 23 '06 #5
cj
Hi gene,

I guess I need to start including that I'm using VB2003. It doesn't
appear to be available in VB2003.
gene kelley wrote:
On Wed, 22 Mar 2006 17:02:01 -0500, cj <cj@nospam.nospam> wrote:
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
.
.
.
.
cj wrote:
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.

cj wrote:
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

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

Mar 23 '06 #6
cj
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.
cj wrote:
Hi gene,

I guess I need to start including that I'm using VB2003. It doesn't
appear to be available in VB2003.
gene kelley wrote:
On Wed, 22 Mar 2006 17:02:01 -0500, cj <cj@nospam.nospam> wrote:
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
.
.
.
.
cj wrote:
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.

cj wrote:
> 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

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

Mar 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
10
Niheel
by: Niheel | last post by:
Web 2.0 is this big buzz word that describes what is going on with internet technology today. It isn't this any one thing. Like many of you, when I first heard it, I was confused. Immediately...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
10
by: silverburgh.meryl | last post by:
Hi, Can you please tell me what is the mean of 't' in the input parametre in utf8_fopen? utf8_fopen( a_file, "wt" ) Thank you.
7
by: Giancarlo Bassi | last post by:
Please, what are here the 11 include files (found over the internet)? */mozzarella.c /* #include #include #include #include #include #include
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.