XML library

agraham

Expert
Licensed User
Longtime User
The XML library provides an XMLReader object that can read XML data from a suitably formatted file and an XMLWriter object that can write XML data to a file. Actually these objects are thin wrappers that expose the .NET XmlReader and XmlWriter classes to Basic4ppc. This library requires .NET 2.0 or later.

There is a (sort of - I don't really understand XML) demo, help and source for merging in the archive. If anyone who understands XML needs more functionality then post here and I will try to add it. If you can reference the MSDN docs on XMLReader XmlReader Members (System.Xml) and XmlWriter Members (System.Xml) then that would be useful.

EDIT :- Rapid update to version 1.1 with ReadString added to XMLReader and WriteCData added to XMLWriter as suggested by DaveW and corwin42. Thanks to both.

EDIT:- Version 1.2 posted with some additional navigational methods exposed for XmlReader See post #13.

EDIT:- Version 2.0 posted with some additional overloads for XmlWriter.WriteStartElement exposed and new XmlDocument and XmlNode objects. The XmlDocumentDemo uses StringBuilder from my http://www.b4x.com/forum/additional-libraries/2022-stringsex-library.html#post10957 See posts #24 and #25

EDIT:- Minor update to version 2.1. See post#27

EDIT:- Updated to version 2.2. See post #33.

EDIT:- Updated to version 2.3. See post #39.

EDIT:- Updated to version 2.4. See post #43.

EDIT:- Updated to version 2.5. See post #59.

EDIT:- Updated to version 2.6. See post #61.
 

Attachments

  • XML2.6.zip
    36.9 KB · Views: 343
Last edited:

DaveW

Active Member
Licensed User
Longtime User
Hi Andrew,

Something odd happened overnight with the XMLReader! When I first started playing with it, for NodeType: Element, rdr.Value returned the text content of the element. I *know* it did, I have the code here and was loading text from XML files quite happily!

But this morning that no longer works. Did you change that in v0.2? Now the rdr.Value only returns the text when the NodeType is Text. The trouble with that is you don't know what element that text came from (rdr.name is empty).

I had a look at the MSDN docs and there is a method called ReadString which (I think) does return the text when the NodeType is Element. Would it be possible to add that to the DLL?

XmlReader.ReadString Method (System.Xml)

Regards,
David.
 

agraham

Expert
Licensed User
Longtime User
When I first started playing with it, for NodeType: Element, rdr.Value returned the text content of the element. ... Did you change that in v0.2?
No, I haven't changed anything. Actually there is little to change as the Basic4ppc XMLReader object just calls the same named methods of the .NET XmlReader class (I don't believe in making things complicated :)). If you look in the help, and the MSDN docs, under Value it appears to be documented that it would return an empty string for "Element". According to that only nine of the node types will return a value and "Element" is not one of them.


Now the rdr.Value only returns the text when the NodeType is Text. The trouble with that is you don't know what element that text came from
As an XML document is a tree structure I assume you could track the current element using "Element" to stack the current element and "EndElement" to unstack it and revert to the previous element.


I'll add ReadString anyway, perhaps corwin42/Markus can comment!
 

DaveW

Active Member
Licensed User
Longtime User
It is really very odd! I know you did not change anything - or take anything out - but I also know that last night it was working and today it did not :( Anyway I have made some simple changes so that now it does work. Thanks anyway!

David.
 

corwin42

Expert
Licensed User
Longtime User
It is not absolutely necessary to have the ReadString() method if you stack the elements as agraham explained but it will make things a bit easier. So it would be a good idea to implement it. The writer has the WriteString() method so perhaps the reader really should have ReadString().

Addidionally I will suggest to implement WriteCData() method to create CDATA Blocks. I don't really need it but it makes the writer more complete.

Greetings,
Markus
 

Elrick

Member
Licensed User
Hello, agraham. In my prog i need to send a XML string to server and then get a response (XML too). Can it be done with this lib? Not to save into file, just get the xml string to send it and then not to open a file, just parse the response string.
 

wbauer

Member
Licensed User
Longtime User
Nice library.:)

My whishes for a next version - navigation in element-tree

Sample XML File:

<books>
..<book id="1" name="basic4pcc for dummies">
....<kindOfCover id="1" kind="softcover" />
....<kindOfCover id="2" kind="hardcover" />
..</book>
..<book id="2" name="c++ for dummies">
....<kindOfCover id="1" kind="softcover" />
..</book>
</books>

(.. just for formating purpose)

possible navigation commands:


  • GetNextSibling ... get the next element of the same type (next "book")
  • GetFirstChild ... get the first subelement (e.g. first "kindOfCover" from actual "book")
  • GetParent ... get the parent of the actual subelement - e.g. get parent ("book" with id=1 ) for "hardcover" (kindOfCover with id=2)

This would help to navigate in a XML File

Your's
Wolfgang
 

wbauer

Member
Licensed User
Longtime User
One more usefull command:

GetPrevSibling ... get the previous sibling (eg. when the actual element is "book" with id=2 the previous sibling would bei the book with id=1)
 

agraham

Expert
Licensed User
Longtime User
I am afraid that backward navigation is not possible with the .NET XmlReader as it, to quote the MSDN docs, "Represents a reader that provides fast, non-cached, forward-only access to XML data.".

I have posted version 1.2 in the first post that exposes what limited navigation is provided by XmlReader. The additions are obvious as they are documented in a separate section of methods in the XmlReader help.

There is an XmlDocument class in .NET that implements implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. From the docs - "The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access. This means that there are no capabilities to edit the values of an attribute or content of an element, or the ability to insert and remove nodes with the XmlReader. Editing is the primary function of the DOM. It is the common and structured way that XML data is represented in memory, although the actual XML data is stored in a linear fashion when in a file or coming in from another object."

At the moment I have no intention of exposing this beast as a Basic4ppc library, let alone writing the help for it!
 

wbauer

Member
Licensed User
Longtime User
I tried to use your library in one of my projects for exporting data from a database into a xml-file . When I run it with Basic4PPC during developing everythings works fine, both on Windows and my device.

But when i compile it and try to execute it on my device, I get an error "System.TypeLoadException" and the program doesn't start. Can't see the details of this error message (Install NETCFv35.Messages.DE.wm.cab ... I have it installed, but i get always this error)

I've a HTC Touch PRO and compile with "Device (Force QVGA) EXE".

Any hints what's wrong?

Wolfgang
 

agraham

Expert
Licensed User
Longtime User
Any hints what's wrong?
The library loads fine on my HTC Diamond WM6.1. Does the demo run OK on your device when optimised compiled? Note that as posted that demo has a missing "&" in line 58 after ": " that the optimising compiler will complain about.

An optimised compiled app doesn't give line numbers when it errors. Using my http://www.b4x.com/forum/share-your-creations/3632-debug-optimised-app-device-desktop.html utility in simple mode will let you identify the line that is causing the problem. You need to comment out a line in the source to suit your OS then optimise compile it and put the exe in your project directory and run it, as an Administrator if you are using Vista. It won't recompile to Force QVGA but to an ordinary exe but it should tell you the line in error.

Alternatively post some source code that shows the error and I will take a look.
 

wbauer

Member
Licensed User
Longtime User
Hi agraham

your demo doest not run on my device, when i compile to Device Force QVGA...

In my example i tried to use only minimal code of your library -> just create a xml file and close it afterwards - and also this crashes on my device. But - this code works fine when compiled to windows and if i compile it to "Device Exe". It also works on my device when I run the code with basic4ppc-interpreter.

B4X:
XMLwrite.New1
XMLwrite.Open(AppPath & "\GPSRoute.kml")
   
   'place code for GPS coordinates here...
   
XMLwrite.Close
Msgbox ("Done")

I tried to use your debug.dll but the crash always occurs right after the start just after the messagebox that i try to use a new dll...

Any hint would help
Wolfgang
 

agraham

Expert
Licensed User
Longtime User
Any hint would help
I am afraid that I cannot help. The demo runs fine on my HTC Diamond (WM 6.1) when Force QVGA compiled, and on my iPAQ 214(WM6.0) and on my Axim X30 (WM2003) so I am fairly sure the problem must be with your device and not the library. Are you using the standard ROM?
 

wbauer

Member
Licensed User
Longtime User
I'm using the standard rom.

Encounter the same problem today with another library (fgControls-Library). The standard libraries of basic4ppc work correctly but I seems, that some of the additional ones don't work correct.

I helped myself by writing the XML Information into a normal txt-File (Filewrite...). Dirty but working.

lg
wolfgang
 

wbauer

Member
Licensed User
Longtime User
Some libraries seem to work wrong when I execute programs on my device, that I've compiled as Force QVGA, but not every library.

I solved the problem with the xml.dll for me now by using Fillipo's function ChangeToVGA and compiling as "normal" device exe. Then the xml dll works correctly.

Thanks for your support
Wolfgang
 
Top