xsharp.eu • XMLParser
Page 1 of 1

XMLParser

Posted: Wed Nov 21, 2018 2:16 pm
by Intexso
Hello,

A long time ago I got a class named XMLParser from someone. Unfortunately I have not documented the source.

With this class I can easily build, store and read XML files. I have the source code for this class (in VO).
In this class much of the type PTR is used and I also see a lot of casts. Several functions now expect PSZ in Vulcan (and X #) and that is why I think the code needs to be rewritten properly.

Does anyone know if an XMLParser class is available somewhere (or maybe an assembly exists) that can be used to build, store and read XML files?

Thank is advance for your answer.

Eric

XMLParser

Posted: Wed Nov 21, 2018 2:24 pm
by wriedmann
Hi Eric,

you can use the XML parser classes in the .NET Framework: XmlNode and XmlDocument in the System.XML namespace. I would not use VO classes if possible (I have done the inverse thing: I'm using the .NET classes in my VO application through a COM DLL).
https://docs.microsoft.com/en-us/dotnet ... mldocument

Wolfgang

XMLParser

Posted: Wed Nov 21, 2018 2:25 pm
by Chris
Yeah, please check the documentation about System.Xml namespace, System.Xml.XmlDocument etc. You only need to add a reference to System.Xml library to use them

https://docs.microsoft.com/en-us/dotnet ... work-4.7.1

https://docs.microsoft.com/en-us/dotnet ... work-4.7.1

Edit: I am way slower than Wolfgang :)

XMLParser

Posted: Wed Nov 21, 2018 2:47 pm
by Intexso
Thanks (to both)!!!

XMLParser

Posted: Thu Nov 22, 2018 6:27 am
by lumberjack
Hi Eric,
Intexso wrote: Does anyone know if an XMLParser class is available somewhere (or maybe an assembly exists) that can be used to build, store and read XML files?
Here is a reader and writer for Xml. Methods from my IniFile to convert Ini files to Xml format. Should get you going.

Code: Select all

METHOD ReadXml() AS VOID
	LOCAL sFileXml AS STRING
	LOCAL xr AS XmlReader
	LOCAL settings AS XmlReaderSettings
	LOCAL s, n, v AS STRING
	sFileXml := SELF:sFile:Replace(Path.GetExtension(SELF:sFile), ".config")
	IF !File.Exists(sFileXml)
		RETURN
	ENDIF
	settings := XmlReaderSettings{}
	settings:IgnoreWhitespace := TRUE
	xr := XmlReader.Create(sFileXml, settings)
	xr:MoveToContent()
	IF xr:Name == "configuration"
		WHILE xr:Read()
			IF xr:NodeType = XmlNodeType.EndElement
				LOOP
			ENDIF
			IF xr:Name == "section"
				s := xr["name"]
			ELSEIF xr:Name == "entry"
				n := xr["name"]
				v := xr["value"]
				SELF:SetString(s, n, v)
			ENDIF
		ENDDO
	ENDIF
	xr:Close()
RETURN

METHOD FlushXml() AS VOID
	LOCAL sFileXml AS STRING
	LOCAL xw AS XmlTextWriter
	LOCAL s, n, v AS STRING
	LOCAL sSections, sItems AS STRING[]
	LOCAL i, j AS INT
	sFileXml := SELF:sFile:Replace(Path.GetExtension(SELF:sFile), ".config")
	IF File.Exists(sFileXml)
		File.Copy(sFileXml, sFileXml + ".bak", TRUE)
	ENDIF
	xw := XmlTextWriter{sFileXml, Encoding.UTF8}
	xw:Formatting := Formatting.Indented
	xw:WriteStartDocument()
	xw:WriteStartElement("configuration")
	sSections := SELF:GetSectionNames()
	FOR i := 0 UPTO sSections:Length - 1
		s := sSections[i]
		xw:WriteStartElement("section")
		xw:WriteAttributeString("name", s)
		sItems := SELF:GetItemNames(s)
		FOR j := 0 UPTO sItems:Length - 1
			n := sItems[j]
			v := SELF:GetString(s, n)
			xw:WriteStartElement("entry")
			xw:WriteAttributeString("name", n)
			xw:WriteAttributeString("value", v)
			xw:WriteEndElement()
		NEXT
		xw:WriteEndElement()
	NEXT
	xw:WriteEndDocument()
	xw:Close()
RETURN
HTH,