Code Snippets


XML_DeserializeFromString

public object XML_DeserializeFromString(
string XmlOfAnObject,
Type ObjectType)
{
object myObject = null;
var stringReader = new System.IO.StringReader(XmlOfAnObject);
var serializer = new System.Xml.Serialization.XmlSerializer(ObjectType);
var xmlReader = new System.Xml.XmlTextReader(stringReader);

try
{
if (My.Settings.USER_ERROR_OCCURRED)
return null;

myObject = serializer.Deserialize(xmlReader);
return myObject;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
finally
{
xmlReader.Close();
stringReader.Close();
stringReader.Dispose();
}
}

XML_DeserializeFromXmlFile

public object XML_DeserializeFromXmlFile(
string FullFilePath,
Type ObjectType)
{
System.IO.StreamReader reader = null;

try
{
modGeneral.Tracer_AddSubroutineStart(
System.Reflection.MethodBase.GetCurrentMethod());

if (My.Settings.ERROR_OCCURRED)
return null;

var serializer = new System.Xml.Serialization.XmlSerializer(ObjectType);
reader = new System.IO.StreamReader(FullFilePath);

object myObject = serializer.Deserialize(reader);
return myObject;
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"Error deserializing " + FullFilePath + " to " + ObjectType.ToString(),
null,
false);

return null;
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
}

XML_GetStringRepresentationOfXmlFile

public string XML_GetStringRepresentationOfXmlFile(
string FullFilePath)
{
System.IO.StreamReader reader = null;

try
{
modGeneral.Tracer_AddSubroutineStart(
System.Reflection.MethodBase.GetCurrentMethod());

if (My.Settings.ERROR_OCCURRED)
return string.Empty;

reader = new System.IO.StreamReader(FullFilePath);
return reader.ReadToEnd();
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"Error reading xml cache file to string: " + FullFilePath);

return string.Empty;
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
}

XML_GetTable

public static string XML_GetTable(
string sXMLString,
string sTableIdentifier)
{
try
{
if (gbEND_GENERAL)
return string.Empty;

string sidentifier = sTableIdentifier.Replace(" ", "").ToLower();

string startTag = "";
string endTag = "
";

int startPos = sXMLString.IndexOf(startTag);

if (startPos > -1)
{
// Move past the start tag
startPos += startTag.Length;

string remainder = sXMLString.Substring(startPos);

int endPos = remainder.IndexOf(endTag);

if (endPos > -1)
{
return remainder.Substring(0, endPos);
}

return string.Empty;
}
else
{
return string.Empty;
}
}
catch (System.Runtime.InteropServices.COMException comEx)
{
mobjCOMException = comEx;
return string.Empty;
}
catch (Exception ex)
{
mobjException = ex;
return string.Empty;
}
finally
{
if (gbDEBUG_GENERAL ||
mobjCOMException != null ||
mobjException != null)
{
clsError.Handle(
"GetTable",
msCLASSNAME,
".",
mobjCOMException,
mobjException);
}
}
}

XML_SerializeToString

public string XML_SerializeToString(
object TargetObject)
{
var serializer = new System.Xml.Serialization.XmlSerializer(TargetObject.GetType());
var stringWriter = new System.IO.StringWriter();
var xmlWriter = new System.Xml.XmlTextWriter(stringWriter);

try
{
if (My.Settings.USER_ERROR_OCCURRED)
return string.Empty;

serializer.Serialize(xmlWriter, TargetObject);
return stringWriter.ToString();
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
finally
{
stringWriter.Close();
stringWriter.Dispose();
}
}

XML_SerializeToXmlFile

public bool XML_SerializeToXmlFile(
object TargetObject,
string FullFilePath)
{
bool blnSuccessful = false;

try
{
if (My.Settings.USER_ERROR_OCCURRED)
return false;

if (TargetObject != null)
{
var serializer = new System.Xml.Serialization.XmlSerializer(TargetObject.GetType());
using (var fs = new System.IO.FileStream(FullFilePath, System.IO.FileMode.Create))
{
serializer.Serialize(fs, TargetObject);
}

blnSuccessful = true;
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);
}

return blnSuccessful;
}

XML_ToComboBox

public static void XML_ToComboBox(
string sXMLString,
System.Windows.Forms.ComboBox objComboBox,
string sTableIdentifier,
bool bClearList = true)
{
try
{
if (gbEND_GENERAL)
return;

if (sXMLString == "Invalid File")
return;

if (bClearList)
objComboBox.Items.Clear();

// Extract the ... block
sXMLString = clsXML.GetTable(sXMLString, sTableIdentifier);

while (sXMLString.Length > 0)
{
// Move past the opening tag
sXMLString = sXMLString.Substring(sXMLString.IndexOf(">") + 1);

// Extract the item text
string sitem = sXMLString.Substring(0, sXMLString.IndexOf("<"));

// Add to ComboBox
objComboBox.Items.Add(sitem);

// Remove the item text
sXMLString = sXMLString.Substring(sitem.Length);

// Move past the closing tag
sXMLString = sXMLString.Substring(sXMLString.IndexOf(">") + 1);
}
}
catch (System.Runtime.InteropServices.COMException comEx)
{
mobjCOMException = comEx;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL ||
mobjCOMException != null ||
mobjException != null)
{
clsError.Handle(
"ToComboBox",
msCLASSNAME,
".",
mobjCOMException,
mobjException);
}
}
}

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top