Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Read/Write/Remove Create XML

, 25 Aug 2011 CPOL
XML Function to Read/Write/Remove/Create XML File
vbnet_csharp_python_ruby_source_code.zip
require "mscorlib"
require "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
require "System.IO, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
require "System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
require "System.Xml.XPath, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

class Module1 # Private Functions List..... This is were the magic happens =P
	def check_xml_entry(xml_filename, xml_path, value_name)
		begin
			xd = XmlDocument.new()
			xd.Load(xml_filename) # Find the node where the Person's attribute ID is 1 using its XPath.
			nod = xd.SelectSingleNode(xml_path)
			if nod == nil then
				return_value = "True"
			else
				return_value = "False"
			end
			xd.Save(xml_filename)
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def Check_Att(xml_filename, xpath, value_name, att_name)
		begin
			xd = XmlDocument.new()
			xd.Load(xml_filename)
			nod = xd.SelectSingleNode(xpath + "/" + value_name + "[@" + att_name + "]")
			if nod == nil then
				return_value = "True"
			else
				return_value = "False"
			end
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def Out_xml_from_xml_path(xml_path, value_name, value, att_name, att_value)
		if System::String.IsNullOrEmpty(att_name) == false then
			master = "<" + value_name + " " + att_name + "=" + self.Chr(34) + att_value + self.Chr(34) + ">" + value + "</" + value_name + ">"
		else
			master = "<" + value_name + ">" + value + "</" + value_name + ">"
		end
		a = xml_path.Trim("/")
		x = a.IndexOf("/")
		if x < 1 then # Is Root
			return_value = master
		end
		b = a.Remove(0, x + 1)
		d = b
		while 
			x = d.LastIndexOf("/")
			if x < 1 then # Is Last Key
				master = "<" + d + ">" + master + "</" + d + ">"
				return_value = master
			end
			b = d.Remove(0, x + 1) # thats without /
			c = d.Remove(0, x) # thats with /
			master = "<" + b + ">" + master + "</" + b + ">"
			a = d.Replace(c, "")
			d = a
		end
		return master
	end

	def Create_New_XML(xml_filename, xml_path, value_name, value, att_name, att_value)
		begin
			settings = XmlWriterSettings.new()
			settings.Indent = true
			settings.Encoding = System.Text.Encoding.UTF8
			XmlWrt = XmlWriter.Create(xml_filename, settings)
XmlWrt			.WriteStartDocument()
			.WriteComment("XML Document Constructed on " + DateTime.Now.Date + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year)
			.WriteComment("Basic XML File. Create with Code from Dool Cookies")
			.WriteComment("From www.CodeProject.com")
			a = xml_path.Trim("/")
			b = a + "/" + value_name
			enumerator = b.Split("/").GetEnumerator()
			while enumerator.MoveNext()
				t = enumerator.Current
				.WriteStartElement(t)
			end
			if System::String.IsNullOrEmpty(att_name) == false then
				.WriteAttributeString(att_name, att_value)
			end
			.WriteString(value)
			.WriteFullEndElement()
			.WriteEndDocument()
			.Close()
			return_value = true
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def add_to_xml(xml_filename, xml_path, value_name, value)
		begin
			cr = Environment.NewLine
			dool = self.Out_xml_from_xml_path(xml_path, value_name, value, nil, nil)
			xd = XmlDocument.new()
			xd.Load(xml_filename)
			docFrag = xd.CreateDocumentFragment()
			docFrag.InnerXml = dool
			root = xd.DocumentElement
			root.AppendChild(docFrag)
			xd.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def Edit_XML_Entry(xml_filename, xml_path, Value_Name, Value)
		xd = XmlDocument.new()
		xd.Load(xml_filename)
		nod = xd.SelectSingleNode(xml_path + "/" + Value_Name)
		if nod == nil then
			nod.InnerXml = Value
			return_value = "True"
		else
			return_value = "Dool_Cookies"
		end
		xd.Save(xml_filename)
		return return_value
	end

	def add_xml_att(xml_filename, xml_path, value_name, att_name, att_value)
		begin
			document = Xml.XmlDocument.new()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path + "/" + value_name)
			nav.CreateAttribute(nil, att_name, nil, att_value)
			document.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def update_att(xml_filename, xml_path, value_name, att_name, att_value)
		xd = XmlDocument.new()
		xd.Load(xml_filename)
		nod = xd.SelectSingleNode(xml_path + "/" + value_name + "[@" + att_name + "]")
		if nod == nil then
			nod.Attributes.GetNamedItem(att_name).Value = att_value
			return_value = "True"
		else
			self.MsgBox("Opps")
		end
		xd.Save(xml_filename)
		return return_value
	end

	def Get_ATT(xml_Filename, xml_path, value_name, att_name)
		begin
			xd = XmlDocument.new()
			xd.Load(xml_Filename)
			nod = xd.SelectSingleNode(xml_path + "/" + value_name + "[@" + att_name + "]")
			if nod == nil then
				a = nod.Attributes.GetNamedItem(att_name).Value
				return_value = a
			else
				return_value = nil
			end
			xd.Save(xml_Filename)
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def Get_Val(xml_filame, xml_path, value_name)
		begin
			xd = XmlDocument.new()
			xd.Load(xml_filame)
			nod = xd.SelectSingleNode(xml_path + "/" + value_name)
			if nod == nil then
				a = nod.InnerXml
				return_value = a
			else
				return_value = nil
			end
			xd.Save(xml_filame)
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def delete_Element(xml_filename, xml_path, value_name)
		begin
			document = Xml.XmlDocument.new()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path + "/" + value_name)
			nav.DeleteSelf()
			document.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def delete_tree(xml_filename, xml_path)
		begin
			document = Xml.XmlDocument.new()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path)
			nav.DeleteSelf()
			document.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def create_tree(xml_filename, start_at, add_these)
		begin
			document = Xml.XmlDocument.new()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(start_at)
			a = add_these.Trim("/")
			b = start_at
			enumerator = a.Split("/").GetEnumerator()
			while enumerator.MoveNext()
				t = enumerator.Current
				b = b + "/" + t
				nav.AppendChildElement(nil, t, nil, "")
				nav = nav.SelectSingleNode(b)
			end
			document.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end

	def dool_cookies(xml_filename, xml_path, value_name, value)
		begin
			dool = XmlDocument.new()
			dool.Load(xml_filename)
			nav = dool.CreateNavigator
			nav = nav.SelectSingleNode(xml_path)
			nav.AppendChildElement(nil, value_name, nil, value)
			dool.Save(xml_filename)
			return_value = "True"
		rescue Exception => ex
			return_value = ex.Message
		ensure
		end
		return return_value
	end
 # Public Functions List........
	def Write_XML_Value(XML_Filename, XML_Path, Value_Name, Value)
		if File.Exists(XML_Filename) == false then
			a = self.Create_New_XML(XML_Filename, XML_Path, Value_Name, Value, nil, nil)
			return_value = a
		end
		a = self.check_xml_entry(XML_Filename, XML_Path, Value_Name) # Check to see if entry exsists.
		if a.ToLower == "true" then # Yes we need to update the value
			b = self.Edit_XML_Entry(XML_Filename, XML_Path, Value_Name, Value)
			return_value = b
			if b.ToLower == "dool_cookies" then
				c = self.dool_cookies(XML_Filename, XML_Path, Value_Name, Value)
				return_value = c
			end
		else # No we need to make a new value
			b = self.add_to_xml(XML_Filename, XML_Path, Value_Name, Value)
			return_value = b
		end
		return return_value
	end

	def Write_XML_Attribute(XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value)
		if File.Exists(XML_FileName) == false then
			a = self.Create_New_XML(XML_FileName, XML_Path, Value_Name, nil, Attribute_Name, Attribute_Value)
			return_value = a
		end
		a = self.Check_Att(XML_FileName, XML_Path, Value_Name, Attribute_Name)
		if a.ToLower == "true" then # Att does exsist, update
			a = self.update_att(XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value)
			return_value = a
		else # create new one.
			a = self.add_xml_att(XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value)
			return_value = a
		end
		return return_value
	end

	def Read_XML_Value(XML_Filename, XML_Path, Value_Name)
		if File.Exists(XML_Filename) == false then
			return_value = "File Does Not Exist"
		end
		a = self.Get_Val(XML_Filename, XML_Path, Value_Name)
		return_value = a
		return return_value
	end

	def Read_XML_Attribute(XML_Filename, XML_Path, Value_Name, Attribute_Name)
		if File.Exists(XML_Filename) == false then
			return_value = "File Does Not Exist"
		end
		a = self.Get_ATT(XML_Filename, XML_Path, Value_Name, Attribute_Name)
		return_value = a
		return return_value
	end

	def Remove_XML_Entry(XML_Filename, XML_Path, Value_Name)
		if File.Exists(XML_Filename) == false then
			return_value = "File Does Not Exist"
		end
		a = self.delete_Element(XML_Filename, XML_Path, Value_Name)
		return_value = a
		return return_value
	end

	def Remove_From_Element(XML_Filename, XML_Path)
		if File.Exists(XML_Filename) == false then
			return_value = "File Does Not Exist"
		end
		a = self.delete_tree(XML_Filename, XML_Path)
		return_value = a
		return return_value
	end

	def Create_XML_Tree(xml_filename, Create_at_xml_path, Extra_Tree_Elements)
		if File.Exists(xml_filename) == false then
			return_value = "File Does Not Exist"
		end
		a = self.create_tree(xml_filename, Create_at_xml_path, Extra_Tree_Elements)
		return_value = a
		return return_value
	end
end

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dool_Cookies
Other Classified
Australia Australia
Programming for fun =)

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 25 Aug 2011
Article Copyright 2011 by Dool_Cookies
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid