Question about XML Reader
Question about XML Reader
Question about XML Reader
Question about XML Reader
Question about XML Reader
Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader
Question about XML Reader Question about XML Reader
Question about XML Reader
Go Back  Xtreme Visual Basic Talk > > > Question about XML Reader


Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2004, 09:14 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default Question about XML Reader


I have an XML document that has a tag that's both an element and an end element tag (<...\>). The XML Reader only identifies this as an element tag, and not as a end element tag. What can I do about this?
Reply With Quote
  #2  
Old 07-26-2004, 07:27 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

That is quite strange. Can we see the code you are using and how you defined your objects? The XML Reader should be recognizing both of these. What type of schema is this?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 07-26-2004, 09:28 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

Quote:
Originally Posted by MKoslof
That is quite strange. Can we see the code you are using and how you defined your objects? The XML Reader should be recognizing both of these. What type of schema is this?
here is part of the code for the writer:

xtw.WriteStartElement("Ideas")
i = 0
Do While i < pjt.Ideas.Count
xtw.WriteStartElement("Idea")
xtw.WriteElementString("IdeaDate", pjt.Ideas.Item(i).IdeaDate)
xtw.WriteElementString("Source", pjt.Ideas.Item(i).Source)
xtw.WriteElementString("IdeaText", pjt.Ideas.Item(i).Text)
xtw.WriteElementString("IdeaNotes", pjt.Ideas.Item(i).Notes)
xtw.WriteElementString("Feedback", pjt.Ideas.Item(i).Feedback)
xtw.WriteEndElement() 'close Idea
i += 1
Loop
xtw.WriteEndElement() 'close Ideas tag

and here is what never gets executed:

Do While xtr.Read
Select Case xtr.NodeType
Case XmlNodeType.EndElement
Select Case xtr.Name 'turn on related bool
...
Case "Ideas"
....
Reply With Quote
  #4  
Old 07-26-2004, 06:00 PM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Ok, I believe this has to do with how you are physically writing your XML file.But first off, how are you opening the XML document. Here is how I would do something like this:

Code:
Dim reader As New Xml.XmlTextReader("C:\test.xml") 'handle any white space issues reader.WhitespaceHandling = Xml.WhitespaceHandling.None Try While reader.Read() Select Case reader.NodeType Case Xml.XmlNodeType.Element Select Case reader.Name Case "mkoslof" reader.MoveToAttribute("Whatever") reader.ReadAttributeValue() ' do something else, etc End Select Case Xml.XmlNodeType.EndElement If reader.Name = "something else" Then 'do something End If End Select End While Catch ex As Xml.XmlException 'your error handling, etc. End Try
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 07-27-2004, 07:49 PM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

Quote:
Originally Posted by MKoslof
Ok, I believe this has to do with how you are physically writing your XML file.But first off, how are you opening the XML document. Here is how I would do something like this:

Code:
Dim reader As New Xml.XmlTextReader("C:\test.xml") 'handle any white space issues reader.WhitespaceHandling = Xml.WhitespaceHandling.None Try While reader.Read() Select Case reader.NodeType Case Xml.XmlNodeType.Element Select Case reader.Name Case "mkoslof" reader.MoveToAttribute("Whatever") reader.ReadAttributeValue() ' do something else, etc End Select Case Xml.XmlNodeType.EndElement If reader.Name = "something else" Then 'do something End If End Select End While Catch ex As Xml.XmlException 'your error handling, etc. End Try

What exactly are you looking for? I did exactly what you did minus the exception handling and the whitespace stuff
Reply With Quote
  #6  
Old 07-27-2004, 08:15 PM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Again, this SHOULD work . Here is how I would write the XML document...based on the initial code you posted, I am not sure that you are declaring a XMLTextWriter and physically flushing the cache so that you get a proper XML document..and are you using a specific format, such as indenting?

Code:
Dim myWriter As XmlTextWriter = New XmlTextWriter("c:\Test.xml", Nothing) myWriter.Formatting = System.Xml.Formatting.Indented myWriter.WriteStartDocument(False) myWriter.WriteComment("comments here") myWriter.WriteStartElement("Entries") myWriter.WriteStartElement("Entry", Nothing) myWriter.WriteElementString("Date", Text1.Text) myWriter.WriteElementString("MKoslof", Text2.Text) myWriter.WriteEndElement() 'Write the XML to file and close the writer myWriter.Flush() myWriter.Close() 'etc
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 07-28-2004, 07:52 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

Hold on a sec...you're not being clear. What should work, the XMLReader should recognize <...\> as an endelement? Or maybe it should produce a <...><.../> instead?

It sure looks to me like I have a proper XML document.
Here's how I declared my XMLWriter:

Dim xtw As New XmlTextWriter(FilePath, System.Text.Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xtw.Indentation = 2

I am not flushing.

What do mean by "proper XML document"? The document opens up in IE without a problem.
Reply With Quote
  #8  
Old 07-28-2004, 07:56 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Then I don't think you ever actually create the XML document ...if you don't flush the object, you don't create the document.

What should work is: create your XML document with the proper elements. Flush the object. Now stop and check your destination directory. Is the file there? Now, try to read it in.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #9  
Old 07-28-2004, 07:59 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

In addition I recommending looking over this site:

Here is a direct link to writing XML with VB.net

http://samples.gotdotnet.com/quickst...teXMLFile.aspx

This site has tons of examples: writing, reading, appending and all other XML functionality in .Net.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #10  
Old 07-28-2004, 08:16 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

[QUOTE=MKoslof]Then I don't think you ever actually create the XML document ...if you don't flush the object, you don't create the document.

Then what is that XML document in the bin directory with all those tags and information in it?
Reply With Quote
  #11  
Old 07-28-2004, 08:19 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Did you review the link I posted....if so, please let me know if have any questions regarding that. There is a mapped out example for writing and reading XML. Good luck.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #12  
Old 07-28-2004, 08:23 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

You never answered my question...
You said "if you don't flush the object, you don't create the document."
What does that mean, you don't create the document? I certaintly see one there?

If that site answers this question, then tell me, and I'll check it out. Otherwise, answer the question.
Reply With Quote
  #13  
Old 07-28-2004, 08:30 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

An XMLTextWriter is just like a StreamWriter or a standard TextWriter...it takes your text and builds the string value or casted values in memory, when you call the .flush method the memory is released and the output is generated. This is proper practice, so that you ensure the TextWriter object has outputted your changes/information.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #14  
Old 07-28-2004, 09:08 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

Quote:
Originally Posted by MKoslof
An XMLTextWriter is just like a StreamWriter or a standard TextWriter...it takes your text and builds the string value or casted values in memory, when you call the .flush method the memory is released and the output is generated. This is proper practice, so that you ensure the TextWriter object has outputted your changes/information.
I'm glad it's proper practice...maybe i'll even think about doing that.
But first I'd like to know why my code doesn't work.

Point number one: flushing is obviously not my problem, because a XML file is created, and it has information in it. Most of the time, it's also the correct information.

Point number two: when there is no data between myWriter.WriteStartElement("....") and myWriter.WriteEndElement(), the XMLWriter puts a self enclosing tag.

This, to any thinking rational person (and just to make sure, I've asked several), should either be considered an endelement, or both a start and an endelement.

However, the XMLReader calls it a start element alone.

Point number three: flushing doesn't solve my problem.
Reply With Quote
  #15  
Old 07-28-2004, 10:13 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, maybe you are just not understanding me. How have you defined your XML. Basically if you have three start elements you need three end elements, if you are adding file attributes they need to be included within the element strings. By default, you get UTF-8 encoding. Here is an example...first create a simple XML document..notice all my start and end elements:

Code:
Imports System.xml Imports System Imports System.IO Module Module4 'if you signify nothing in the second parameter, 'by default you receive UTF-8 format Public Sub write_XML() 'instaniate new object Dim w As XmlTextWriter = New XmlTextWriter _ ("C:\Testing.xml", Nothing) w.Formatting = System.Xml.Formatting.Indented w.WriteStartDocument(False) w.WriteComment("An example by MKoslof") 'now start the elements 'just a header first w.WriteStartElement("MyTesting") 'add a null value to start, just an example w.WriteStartElement("Element1", Nothing) 'now write out an attribute for this element w.WriteAttributeString("FirstAttribute", "TestingValue") w.WriteAttributeString("SecondAttribute", "AnotherTest") 'create an element string w.WriteElementString("MyTitle", Nothing, "Extreme VB Forum") 'create another start element w.WriteStartElement("MyTesting2", Nothing) w.WriteElementString("firstname", "MK") w.WriteElementString("lastname", "LovesToCode") 'write an end element for this w.WriteEndElement() 'add another element string w.WriteElementString("MyPrice", "7.99") 'close out all elements w.WriteEndElement() w.WriteEndElement() 'Write the XML to file and close w.Flush() w.Close() End Sub

Review the attached output..very simply..there are three basic elements, and each has its end element defined.

Now to read this in, I can do something like this...of course, I don't want value and name all the time, just an example..when I read this in, I get all my end elements, the code catches them. Notice first we look for elements, elements will contain attributes, so then we loop those, only the "Element1" element has file attributes, notice that in my code.

Code:
Public Sub read_XML() Dim reader As XmlTextReader = New XmlTextReader _ ("C:\Testing.xml") 'now read until EOF Do While (reader.Read()) Select Case reader.NodeType ' The node is an Element Case Xml.XmlNodeType.Element MessageBox.Show("<" + reader.Name) ' Read any attributes within this element While (reader.MoveToNextAttribute()) MessageBox.Show(" " & reader.Name & "='" & reader.Value + "'") End While Case Xml.XmlNodeType.EndElement MessageBox.Show(" " & reader.Name & "='" & reader.Value & "'") 'read values if necceasary End Select Loop

This is typically how I do most of my XML. This is just a bare bones example, typicaly I create Persistent XML schemas that correlate with my database import routine, but that is not something we are doing here. Now if we want to focus on rational thinking, let's compare these two methods. How is yours different, if your XML is structured differently, how can we adjust the code. There are tons of XML schemas out there, but it appears you are just creating single elements within UTF-8 standards. How are you nodes being created, etc. And if you do not want to flush your writer, thats fine, I am just making suggestions..I do XML frequently..and here is an example for you to look at.

Good luck.
Attached Files
File Type: zip XML_output.zip (338 Bytes, 13 views)
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #16  
Old 07-28-2004, 10:29 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

As far as I can tell, I've done everything you've done...
But allow me to make one change in your code.

instead of:

w.WriteStartElement("MyTesting2", Nothing)
w.WriteElementString("firstname", "MK")
w.WriteElementString("lastname", "LovesToCode")
'write an end element for this
w.WriteEndElement()

let's try:

w.WriteStartElement("MyTesting2", Nothing)
w.WriteEndElement()

Now instead of getting your desired result, which is:
<MyTesting2>......</MyTesting2>

you will get <MyTesting2/>

Now...what kind of node would call that?
Reply With Quote
  #17  
Old 07-28-2004, 11:02 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, now I understand . Basically the problem is, this is an empty element. Basically you can trap this...as an example, within your read module in the case statement you can trap this:

Code:
Case Xml.XmlNodeType.Element If reader.IsEmptyElement Then MessageBox.Show("YES ITS EMPTY" & reader.Name) End If

See, view these as simply tags..because that is all they are. If you can flag empty nodes, you can skip this section, prompt the user and say "this node is empty" do not add this to some other routine, etc. Your XML is valid, but there is nothing to return within the element. Your code to create the XML is fine, and the fact there is one only close bracket is fine..your XML is still OK. But if you loop all elements an empty element will not return any values..so it is basically skipped over.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #18  
Old 07-28-2004, 11:13 AM
drunknhik drunknhik is offline
Newcomer
 
Join Date: Jul 2004
Posts: 20
Default

Ok thanks. What this means for me, is that I would have to trap every single node to see if it's blank, which I'm not willing to do.

Somebody wrote a program in Access, and i'm rewriting it for them in VB. His original Access database (which my program can import into the program), left me with a lot of annoying problems, such as DBNulls, etc.

I can't be sure, that any user of his program, has filled in every field.

The only thing I can do is implement a "hack" for the problem, but from my perspective, I can't really solve this problem.
Reply With Quote
  #19  
Old 07-28-2004, 11:19 AM
MKoslof's Avatar
MKoslofQuestion about XML Reader MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Well, you can do several things if this is a database related schema

1) Open an ADO recordset of the data you need, ADO is very good with creating XML. Via the recordset objects save method you could save the recordset to an XML schema. Now note, if there are null values, these are still going to come in as null. You would have to create a style sheet or some other process that goes through and changes your dbnull values...

2) Basically, I understand what you are trying to do here. Basically, you want to get something out of the XML document (or you wouldn't be reading it in ). But, if the element is null, there is nothing to return anyway. And if an element is null, there will be no endElement or FileAttributes, etc. So really, you only need to trap this ONCE at the Element level. Because, if the element is empty all other properties are empty as well. So there isn't much code involved with this. Basically, trap at the highest level Case construct, the Element type...if empty, flag this and continue on. If this succeeds, there must be an end element and othe properties within in...so the rest of the case statment can flow as normal..does that make sense.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Question about XML Reader
Question about XML Reader
Question about XML Reader Question about XML Reader
Question about XML Reader
Question about XML Reader
Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader Question about XML Reader
Question about XML Reader
Question about XML Reader
 
Question about XML Reader
Question about XML Reader
 
-->