1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

wrapper for XmlReader and XmlWriter?

Discussion in 'Additional Libraries' started by corwin42, Nov 20, 2008.

  1. corwin42

    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
     
  2. DaveW

    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.
     
  3. agraham

    agraham Expert Licensed User

    I know nothing about XML, all the terms are confusing to me, but after a bit of hacking around this sort of seems to do something that looks a bit like XML. Needs .NET 2.0.

    Have a look, if you can understand what you need then I can add it, particularly if you can reference the MSDN docs on XMLReader XmlReader Members (System.Xml) and XMLWriter XmlWriter Members (System.Xml)
     

    Attached Files:

  4. DaveW

    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.
     
  5. agraham

    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:
     

    Attached Files:

  6. corwin42

    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:

    Code:
    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

    Code:
    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
     
  7. agraham

    agraham Expert Licensed User

    Ah good! Somebody seems to know what this XML stuff is all about.
    OK. So you at least want MoveToNextAttribute. Anything else that could be useful?
    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
    Code:
    Select rdr.NodeType
       
    Case "Text"
          ...
       
    Case "Element"
          ...
    End Select
    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.
     
  8. DaveW

    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.


    Code:
    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: Nov 20, 2008
  9. DaveW

    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:
     
  10. corwin42

    corwin42 Expert 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.

    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
     
  11. agraham

    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.

    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.
     
  12. DaveW

    DaveW Active Member Licensed User

    Fair Point :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice