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
from System import *
from System.IO import *
from System.Xml import *
from System.Xml.XPath import *

class Module1(object): # Private Functions List..... This is were the magic happens =P
	def check_xml_entry(self, xml_filename, xml_path, value_name):
		try:
			xd = XmlDocument()
			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 == None:
				return_value = "True"
			else:
				return_value = "False"
			xd.Save(xml_filename)
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def Check_Att(self, xml_filename, xpath, value_name, att_name):
		try:
			xd = XmlDocument()
			xd.Load(xml_filename)
			nod = xd.SelectSingleNode(xpath + "/" + value_name + "[@" + att_name + "]")
			if nod == None:
				return_value = "True"
			else:
				return_value = "False"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def Out_xml_from_xml_path(self, xml_path, value_name, value, att_name, att_value):
		if str.IsNullOrEmpty(att_name) == False:
			master = "<" + value_name + " " + att_name + "=" + self.Chr(34) + att_value + self.Chr(34) + ">" + value + "</" + value_name + ">"
		else:
			master = "<" + value_name + ">" + value + "</" + value_name + ">"
		a = xml_path.Trim("/")
		x = a.IndexOf("/")
		if x < 1: # Is Root
			return_value = master
		b = a.Remove(0, x + 1)
		d = b
		while :
			x = d.LastIndexOf("/")
			if x < 1: # Is Last Key
				master = "<" + d + ">" + master + "</" + d + ">"
				return_value = master
			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
		return master

	def Create_New_XML(self, xml_filename, xml_path, value_name, value, att_name, att_value):
		try:
			settings = XmlWriterSettings()
			settings.Indent = True
			settings.Encoding = System.Text.Encoding.UTF8
			XmlWrt = XmlWriter.Create(xml_filename, settings)
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def add_to_xml(self, xml_filename, xml_path, value_name, value):
		try:
			cr = Environment.NewLine
			dool = self.Out_xml_from_xml_path(xml_path, value_name, value, None, None)
			xd = XmlDocument()
			xd.Load(xml_filename)
			docFrag = xd.CreateDocumentFragment()
			docFrag.InnerXml = dool
			root = xd.DocumentElement
			root.AppendChild(docFrag)
			xd.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def Edit_XML_Entry(self, xml_filename, xml_path, Value_Name, Value):
		xd = XmlDocument()
		xd.Load(xml_filename)
		nod = xd.SelectSingleNode(xml_path + "/" + Value_Name)
		if nod == None:
			nod.InnerXml = Value
			return_value = "True"
		else:
			return_value = "Dool_Cookies"
		xd.Save(xml_filename)
		return return_value

	def add_xml_att(self, xml_filename, xml_path, value_name, att_name, att_value):
		try:
			document = Xml.XmlDocument()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path + "/" + value_name)
			nav.CreateAttribute(None, att_name, None, att_value)
			document.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

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

	def Get_ATT(self, xml_Filename, xml_path, value_name, att_name):
		try:
			xd = XmlDocument()
			xd.Load(xml_Filename)
			nod = xd.SelectSingleNode(xml_path + "/" + value_name + "[@" + att_name + "]")
			if nod == None:
				a = nod.Attributes.GetNamedItem(att_name).Value
				return_value = a
			else:
				return_value = None
			xd.Save(xml_Filename)
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def Get_Val(self, xml_filame, xml_path, value_name):
		try:
			xd = XmlDocument()
			xd.Load(xml_filame)
			nod = xd.SelectSingleNode(xml_path + "/" + value_name)
			if nod == None:
				a = nod.InnerXml
				return_value = a
			else:
				return_value = None
			xd.Save(xml_filame)
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def delete_Element(self, xml_filename, xml_path, value_name):
		try:
			document = Xml.XmlDocument()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path + "/" + value_name)
			nav.DeleteSelf()
			document.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def delete_tree(self, xml_filename, xml_path):
		try:
			document = Xml.XmlDocument()
			document.Load(xml_filename)
			nav = document.CreateNavigator
			nav = nav.SelectSingleNode(xml_path)
			nav.DeleteSelf()
			document.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def create_tree(self, xml_filename, start_at, add_these):
		try:
			document = Xml.XmlDocument()
			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(None, t, None, "")
				nav = nav.SelectSingleNode(b)
			document.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value

	def dool_cookies(self, xml_filename, xml_path, value_name, value):
		try:
			dool = XmlDocument()
			dool.Load(xml_filename)
			nav = dool.CreateNavigator
			nav = nav.SelectSingleNode(xml_path)
			nav.AppendChildElement(None, value_name, None, value)
			dool.Save(xml_filename)
			return_value = "True"
		except Exception, ex:
			return_value = ex.Message
		finally:
		return return_value
 # Public Functions List........
	def Write_XML_Value(self, XML_Filename, XML_Path, Value_Name, Value):
		if File.Exists(XML_Filename) == False:
			a = self.Create_New_XML(XML_Filename, XML_Path, Value_Name, Value, None, None)
			return_value = a
		a = self.check_xml_entry(XML_Filename, XML_Path, Value_Name) # Check to see if entry exsists.
		if a.ToLower == "true": # 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":
				c = self.dool_cookies(XML_Filename, XML_Path, Value_Name, Value)
				return_value = c
		else: # No we need to make a new value
			b = self.add_to_xml(XML_Filename, XML_Path, Value_Name, Value)
			return_value = b
		return return_value

	def Write_XML_Attribute(self, XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value):
		if File.Exists(XML_FileName) == False:
			a = self.Create_New_XML(XML_FileName, XML_Path, Value_Name, None, Attribute_Name, Attribute_Value)
			return_value = a
		a = self.Check_Att(XML_FileName, XML_Path, Value_Name, Attribute_Name)
		if a.ToLower == "true": # 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
		return return_value

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

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

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

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

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

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
Web04 | 2.8.141220.1 | Last Updated 25 Aug 2011
Article Copyright 2011 by Dool_Cookies
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid