XMLParser

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
Intexso
Posts: 84
Joined: Mon Sep 10, 2018 3:19 pm
Location: Nederland

XMLParser

Post 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
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

XMLParser

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4905
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

XMLParser

Post 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 :)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Intexso
Posts: 84
Joined: Mon Sep 10, 2018 3:19 pm
Location: Nederland

XMLParser

Post by Intexso »

Thanks (to both)!!!
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

XMLParser

Post 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,
______________________
Johan Nel
Boshof, South Africa
Post Reply