Click here to Skip to main content
12,295,252 members (58,175 online)
Click here to Skip to main content

Tagged as

Stats

33.8K views
2.1K downloads
25 bookmarked
Posted

Read/Write/Remove Create XML

, 25 Aug 2011 CPOL
XML Function to Read/Write/Remove/Create XML File
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.XPath;

static class Module1
{
	// Private Functions List..... This is were the magic happens =P
	private static string check_xml_entry(string xml_filename, string xml_path, string value_name)
	{
		string return_value = null;
		try {
			XmlDocument xd = new XmlDocument();
			xd.Load(xml_filename);
			// Find the node where the Person's attribute ID is 1 using its XPath.
			XmlNode nod = xd.SelectSingleNode(xml_path);
			if (nod != null) {
				return_value = "True";
			} else {
				return_value = "False";
			}
			xd.Save(xml_filename);
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string Check_Att(string xml_filename, string xpath, string value_name, string att_name)
	{
		string return_value = null;
		try {
			XmlDocument xd = new XmlDocument();
			xd.Load(xml_filename);
			XmlNode nod = xd.SelectSingleNode(xpath + "/" + value_name + "[@" + att_name + "]");
			if (nod != null) {
				return_value = "True";
			} else {
				return_value = "False";
			}
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string Out_xml_from_xml_path(string xml_path, string value_name, string value, string att_name, string att_value)
	{
		string return_value = null;
		string a = null;
		string b = null;
		string c = null;
		string d = null;
		int x = 0;
		int y = 0;
		int z = 0;
		string master = null;
		string buffer = null;
		if (string.IsNullOrEmpty(att_name) == false) {
			master = "<" + value_name + " " + att_name + "=" + Strings.Chr(34) + att_value + Strings.Chr(34) + ">" + value + "</" + value_name + ">";
		} else {
			master = "<" + value_name + ">" + value + "</" + value_name + ">";
		}
		a = xml_path.Trim("/");
		x = a.IndexOf("/");
		// Is Root
		if (x < 1) {
			return_value = master;
			goto 1;
		}
		b = a.Remove(0, x + 1);
		d = b;
		do {
			x = d.LastIndexOf("/");
			// Is Last Key
			if (x < 1) {
				master = "<" + d + ">" + master + "</" + d + ">";
				return_value = master;
				break; // TODO: might not be correct. Was : Exit Do
			}
			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;
		} while (true);
		1:
		return master;
	}
	private static string Create_New_XML(string xml_filename, string xml_path, string value_name, string value, string att_name, string att_value)
	{
		string return_value = null;
		try {
			XmlWriterSettings settings = new XmlWriterSettings();
			settings.Indent = true;
			settings.Encoding = System.Text.Encoding.UTF8;
			string a = null;
			string b = null;
			string c = null;
			string d = null;
			XmlWriter XmlWrt = XmlWriter.Create(xml_filename, settings);
			var _with1 = XmlWrt;
			_with1.WriteStartDocument();
			_with1.WriteComment("XML Document Constructed on " + DateTime.Now.Date + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year);
			_with1.WriteComment("Basic XML File. Create with Code from Dool Cookies");
			_with1.WriteComment("From www.CodeProject.com");
			a = xml_path.Trim("/");
			b = a + "/" + value_name;
			foreach (string t in b.Split("/")) {
				_with1.WriteStartElement(t);
			}
			if (string.IsNullOrEmpty(att_name) == false) {
				_with1.WriteAttributeString(att_name, att_value);
			}
			_with1.WriteString(value);
			_with1.WriteFullEndElement();
			_with1.WriteEndDocument();
			_with1.Close();
			return_value = true;
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string add_to_xml(string xml_filename, string xml_path, string value_name, string value)
	{
		string return_value = null;
		try {
			string cr = Environment.NewLine;
			string dool = null;
			dool = Out_xml_from_xml_path(xml_path, value_name, value, null, null);
			XmlDocument xd = new XmlDocument();
			xd.Load(xml_filename);
			XmlDocumentFragment docFrag = xd.CreateDocumentFragment();
			docFrag.InnerXml = dool;
			XmlNode root = xd.DocumentElement;
			root.AppendChild(docFrag);
			xd.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string Edit_XML_Entry(string xml_filename, string xml_path, string Value_Name, string Value)
	{
		string return_value = null;
		XmlDocument xd = new XmlDocument();
		xd.Load(xml_filename);
		XmlNode nod = xd.SelectSingleNode(xml_path + "/" + Value_Name);
		if (nod != null) {
			nod.InnerXml = Value;
			return_value = "True";
		} else {
			return_value = "Dool_Cookies";
		}
		xd.Save(xml_filename);
		return return_value;
	}
	private static string add_xml_att(string xml_filename, string xml_path, string value_name, string att_name, string att_value)
	{
		string return_value = null;
		try {
			System.Xml.XmlDocument document = new System.Xml.XmlDocument();
			document.Load(xml_filename);
			System.Xml.XPath.XPathNavigator nav = document.CreateNavigator();
			nav = nav.SelectSingleNode(xml_path + "/" + value_name);
			nav.CreateAttribute(null, att_name, null, att_value);
			document.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string update_att(string xml_filename, string xml_path, string value_name, string att_name, string att_value)
	{
		string return_value = null;
		XmlDocument xd = new XmlDocument();
		xd.Load(xml_filename);
		XmlNode nod = xd.SelectSingleNode(xml_path + "/" + value_name + "[@" + att_name + "]");
		if (nod != null) {
			nod.Attributes.GetNamedItem(att_name).Value = att_value;
			return_value = "True";
		} else {
			Interaction.MsgBox("Opps");
		}
		xd.Save(xml_filename);
		return return_value;
	}
	private static string Get_ATT(string xml_Filename, string xml_path, string value_name, string att_name)
	{
		string return_value = null;
		try {
			string a = null;
			XmlDocument xd = new XmlDocument();
			xd.Load(xml_Filename);
			XmlNode nod = xd.SelectSingleNode(xml_path + "/" + value_name + "[@" + att_name + "]");
			if (nod != null) {
				a = nod.Attributes.GetNamedItem(att_name).Value;
				return_value = a;
			} else {
				return_value = null;
			}
			xd.Save(xml_Filename);
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string Get_Val(string xml_filame, string xml_path, string value_name)
	{
		string return_value = null;
		try {
			string a = null;
			XmlDocument xd = new XmlDocument();
			xd.Load(xml_filame);
			XmlNode nod = xd.SelectSingleNode(xml_path + "/" + value_name);
			if (nod != null) {
				a = nod.InnerXml;
				return_value = a;
			} else {
				return_value = null;
			}
			xd.Save(xml_filame);
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;

	}
	private static string delete_Element(string xml_filename, string xml_path, string value_name)
	{
		string return_value = null;
		try {
			System.Xml.XmlDocument document = new System.Xml.XmlDocument();
			document.Load(xml_filename);
			System.Xml.XPath.XPathNavigator nav = document.CreateNavigator();
			nav = nav.SelectSingleNode(xml_path + "/" + value_name);
			nav.DeleteSelf();
			document.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string delete_tree(string xml_filename, string xml_path)
	{
		string return_value = null;
		try {
			System.Xml.XmlDocument document = new System.Xml.XmlDocument();
			document.Load(xml_filename);
			System.Xml.XPath.XPathNavigator nav = document.CreateNavigator();
			nav = nav.SelectSingleNode(xml_path);
			nav.DeleteSelf();
			document.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string create_tree(string xml_filename, string start_at, string add_these)
	{
		string return_value = null;
		string a = null;
		string b = null;
		string c = null;
		string d = null;
		try {
			System.Xml.XmlDocument document = new System.Xml.XmlDocument();
			document.Load(xml_filename);
			System.Xml.XPath.XPathNavigator nav = document.CreateNavigator();
			nav = nav.SelectSingleNode(start_at);
			a = add_these.Trim("/");
			b = start_at;
			foreach (string t in a.Split("/")) {
				b = b + "/" + t;
				nav.AppendChildElement(null, t, null, "");
				nav = nav.SelectSingleNode(b);
			}
			document.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	private static string dool_cookies(string xml_filename, string xml_path, string value_name, string value)
	{
		string return_value = null;
		try {
			XmlDocument dool = new XmlDocument();
			dool.Load(xml_filename);
			System.Xml.XPath.XPathNavigator nav = dool.CreateNavigator();
			nav = nav.SelectSingleNode(xml_path);
			nav.AppendChildElement(null, value_name, null, value);
			dool.Save(xml_filename);
			return_value = "True";
		} catch (Exception ex) {
			return_value = ex.Message;
		}
		return return_value;
	}
	// Public Functions List........
	public static string Write_XML_Value(string XML_Filename, string XML_Path, string Value_Name, string Value)
	{
		string return_value = null;
		string a = null;
		string b = null;
		string c = null;
		string d = null;
		if (File.Exists(XML_Filename) == false) {
			a = Create_New_XML(XML_Filename, XML_Path, Value_Name, Value, null, null);
			return_value = a;
			goto 1;
		}
		a = check_xml_entry(XML_Filename, XML_Path, Value_Name);
		// Check to see if entry exsists.



		// Yes we need to update the value
		if (a.ToLower() == "true") {
			b = Edit_XML_Entry(XML_Filename, XML_Path, Value_Name, Value);
			return_value = b;
			if (b.ToLower() == "dool_cookies") {
				c = dool_cookies(XML_Filename, XML_Path, Value_Name, Value);
				return_value = c;
			}


		// No we need to make a new value
		} else {
			b = add_to_xml(XML_Filename, XML_Path, Value_Name, Value);
			return_value = b;
		}
		1:
		return return_value;
	}
	public static string Write_XML_Attribute(string XML_FileName, string XML_Path, string Value_Name, string Attribute_Name, string Attribute_Value)
	{
		string return_value = null;
		string a = null;
		string b = null;
		string c = null;
		if (File.Exists(XML_FileName) == false) {
			a = Create_New_XML(XML_FileName, XML_Path, Value_Name, null, Attribute_Name, Attribute_Value);
			return_value = a;
			goto 1;
		}
		a = Check_Att(XML_FileName, XML_Path, Value_Name, Attribute_Name);
		// Att does exsist, update
		if (a.ToLower() == "true") {
			a = update_att(XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value);
			return_value = a;
		// create new one.
		} else {
			a = add_xml_att(XML_FileName, XML_Path, Value_Name, Attribute_Name, Attribute_Value);
			return_value = a;
		}
		1:
		return return_value;
	}
	public static string Read_XML_Value(string XML_Filename, string XML_Path, string Value_Name)
	{
		string return_value = null;
		string a = null;
		if (File.Exists(XML_Filename) == false) {
			return_value = "File Does Not Exist";
			goto 1;
		}
		a = Get_Val(XML_Filename, XML_Path, Value_Name);
		return_value = a;
		1:
		return return_value;
	}
	public static string Read_XML_Attribute(string XML_Filename, string XML_Path, string Value_Name, string Attribute_Name)
	{
		string return_value = null;
		string a = null;
		if (File.Exists(XML_Filename) == false) {
			return_value = "File Does Not Exist";
			goto 1;
		}
		a = Get_ATT(XML_Filename, XML_Path, Value_Name, Attribute_Name);
		return_value = a;
		1:
		return return_value;
	}
	public static string Remove_XML_Entry(string XML_Filename, string XML_Path, string Value_Name)
	{
		string return_value = null;
		string a = null;
		if (File.Exists(XML_Filename) == false) {
			return_value = "File Does Not Exist";
			goto 1;
		}
		a = delete_Element(XML_Filename, XML_Path, Value_Name);
		return_value = a;
		1:
		return return_value;
	}
	public static string Remove_From_Element(string XML_Filename, string XML_Path)
	{
		string return_value = null;
		string a = null;
		if (File.Exists(XML_Filename) == false) {
			return_value = "File Does Not Exist";
			goto 1;
		}
		a = delete_tree(XML_Filename, XML_Path);
		return_value = a;
		1:
		return return_value;
	}
	public static string Create_XML_Tree(string xml_filename, string Create_at_xml_path, string Extra_Tree_Elements)
	{
		string return_value = null;
		string a = null;
		if (File.Exists(xml_filename) == false) {
			return_value = "File Does Not Exist";
			goto 1;
		}
		a = create_tree(xml_filename, Create_at_xml_path, Extra_Tree_Elements);
		return_value = a;
		1:
		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
Classified
Australia Australia
Programming for fun =)

You may also be interested in...

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