wrapper for XmlReader and XmlWriter?

corwin42

Expert
Licensed User
I have to parse XML files (.gpx in my case) and I don't want to invent the wheel again.

If I'm not totally wrong, .NET CF has the XmlReader and XmlWriter classes for simple reading and writing of XML files.

If so, would it be hard to write a wrapper dll for this classes? I have no experience with writing dlls so please can someone give me some hints (or even better write such a dll :) )? agraham?

Thanks very much,
Markus
 

DaveW

Active Member
Licensed User
Hi Markus,

I think your suggestion is an interesting one. However, if you need something to get you going now I have written some functions based on RegEx that extract XML Elements and Attributes. Let me know if you would like the code.

David.
 

DaveW

Active Member
Licensed User
Andrew,

As always you are amazing! :sign0098: does not even begin to cover it!

I have run your little test and it seems to work, I will try to replace my existing code with your dll to see how it works in the real world.

David.
 

agraham

Expert
Licensed User
A bit more stuff that looked like it might be useful in the dll and a rudimentary help file mostly cribbed from MSDN. I hope you all understand it - I don't :confused:
 

Attachments

corwin42

Expert
Licensed User
Thank you very much. I was just reading the threads which explain how to compile dlls with SharpDevelop when I saw your posts.

The Reader needs the ability to parse Attributes. Therefore it needs at least the MoveToNextAttribute Method. Then you can walk through the Attributes in your example:

B4X:
Sub ReadXML
   rdr.Open(AppPath & "\test.xml")   
   rdr.Read
   Do
      If rdr.AttributeCount > 0 Then
         Do While rdr.MoveToNextAttribute
            msg = msg & "AttribName: " & rdr.Name & " AttribValue: " & rdr.Value & CRLF
         Loop
      End If
      msg = msg & rdr.NodeType  & ": " rdr.Name & " " & rdr.Value & CRLF
   Loop Until rdr.Read = False
   Msgbox(msg)
End Sub
Is it possible to predefine constants for the values in rdr.NodeType? Normally they are defined in Xml.XmlNodeType.xxx so you can create a Select statement with something like

B4X:
Select rdr.NodeType
   Case cXmlNodeTypeText
      ...
   Case cXmlNodeTypeElement
      ...
End Select
The help file does not work. I only get the message "The page cannot be displayed".

Greetings and many thanks,
Markus
 

agraham

Expert
Licensed User
Ah good! Somebody seems to know what this XML stuff is all about.
The Reader needs the ability to parse Attributes. Therefore it needs at least the MoveToNextAttribute Method. Then you can walk through the Attributes in your example
OK. So you at least want MoveToNextAttribute. Anything else that could be useful?
Is it possible to predefine constants for the values in rdr.NodeType? Normally they are defined in Xml.XmlNodeType.xxx so you can create a Select statement
I am returning the XmlNodeType enum as strings as (once you get the help working) detailed in the help topic "Node types" so you can do
B4X:
Select rdr.NodeType
   Case "Text"
      ...
   Case "Element"
      ...
End Select
The help file does not work. I only get the message "The page cannot be displayed".
Works fine here, it's no different to my many other help files, all written off the same template. Are you running Vista? I had a similar problem recently with Erels help for GPSDriver - turned out Vista security had blocked it as it was an Internet download and I had to Right-click -> Properties -> Unblock to get it to work.
 

DaveW

Active Member
Licensed User
Hi Corwin,

I have just converted my XML reader to use Andrew's DLL and it works fine. Maybe I am not understanding your request, but I don't think the DLL needs the MoveToNextAttribute method. The main loop returns each element in turn. As an element is found, the Item method will give you whatever attribute within the element that you require.


B4X:
Do
If (rdr.NodeType = "Element") AND (rdr.Name = "Book") Then
   bookdate = rdr.Item("Date")
Else If (rdr.NodeType = "Element") AND (rdr.Name = "Chapter") Then
   chap.nbr = rdr.Item("Number")
   chap.name = rdr.Item("Name")
   chap.date = rdr.Item("Date")
Else If (rdr.NodeType = "Element") AND (rdr.Name = "section") Then
           ..... etc.....
End If
Loop Until rdr.Read = False
By the way, using the DLL is 3 times faster than my old RegEx-based code :)
Well done Andrew!
 
Last edited:

DaveW

Active Member
Licensed User
Andrew, Just saw you had replied while I was putting my reply together.:)

The help works for me (though I think that in the Reader section the NodeType list should be under NodeType, not Name).

Again, thanks for a great job :sign0188:
 

corwin42

Expert
Licensed User
Maybe I am not understanding your request, but I don't think the DLL needs the MoveToNextAttribute method. The main loop returns each element in turn. As an element is found, the Item method will give you whatever attribute within the element that you require.
Yes, but if I don't know the name of the attribute I can't access it with .Item. There should be a Method to walk through all attributes.

But there was still a mistake in my example code. The "If rdr.AttributeCount ... End If" block must be below the "msg = msg & rdr.NodeType & ": " rdr.Name & " " & rdr.Value & CRLF" line.

@agraham:
I think with the MoveToNextAttribute we have erverything to parse any xml-File. The writer works for simple XML-Files and thats enough for me. If you supply the source of the dll I will try to get it compiled with SharpDevelop and if I need additional methods I can write them by my own.

The help file works on my PC at work. Seems to be really a security problem because when I want to open it on my home PC WindowsXP asks if I really will open it.

Thank you very much.

I think this library will help many B4PPC users parsing xml files.

Greetings,
Markus
 

agraham

Expert
Licensed User
I have posted the modified library and help as version 1.0 in a new thread so that I can keep the latest version in the first post as usual.

If you supply the source of the dll I will try to get it compiled with SharpDevelop and if I need additional methods I can write them by my own.
Source is in the archive for you to play with. However if you find a need for something extra then maybe someone else will share that need so let me know if you do and I will add it to this library and help file.
 

DaveW

Active Member
Licensed User
Yes, but if I don't know the name of the attribute I can't access it with .Item. There should be a Method to walk through all attributes.
Fair Point :)
 
Top