//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region GetIntAttrib(XmlNode node, string attrib, int defVal) public static int GetIntAttrib(XmlNode node, string attrib, int defVal) { XmlAttribute xmlAttrib = node.Attributes[attrib]; if (xmlAttrib == null) return defVal; string val = xmlAttrib.Value; if (val == null || val == string.Empty) return defVal; int returnVal = defVal; int.TryParse(val, out returnVal); return returnVal; } #endregion } }
XML-RPC
Get Bool Attribute Or Throw exception
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { public static string GetAttrib(XmlNode node, string attrib, string defVal) { XmlAttribute xmlAttrib = node.Attributes[attrib]; if (xmlAttrib == null) return defVal; string val = xmlAttrib.Value; return (val == null) ? defVal : val; } #region GetBoolAttribOrThrow(XmlNode node, string attrib) public static bool GetBoolAttribOrThrow(XmlNode node, string attrib) { string val = GetAttrib(node, attrib, null); if (val == null) throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name)); if (val == null || val == string.Empty) return false; bool returnVal = (val == "1" || val.ToLower() == "true"); return returnVal; } #endregion } }
Get attribute or return default value
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region GetAttrib(XmlNode node, string attrib, string defVal) public static string GetAttrib(XmlNode node, string attrib, string defVal) { XmlAttribute xmlAttrib = node.Attributes[attrib]; if (xmlAttrib == null) return defVal; string val = xmlAttrib.Value; return (val == null) ? defVal : val; } #endregion } }
Get Int value from xml attribute
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region GetIntAttrib(XmlNode node, string name, out bool found, out bool valid, ref int result) public static void GetIntAttrib(XmlNode node, string name, out bool found, out bool valid, ref int result) { if (node.Attributes == null || node.Attributes[name] == null) { found = false; valid = false; return; } found = true; valid = Int32.TryParse(node.Attributes[name].InnerText, out result); } #endregion } }
Get Attribute Value
//Microsoft Public License (Ms-PL) //http://dbmlmanager.codeplex.com/license #region using using System; using System.Xml; #endregion namespace DbmlManager.Lib.Utility { #region Class Docs /// <summary> /// Summary description for XmlUtil. /// </summary> #endregion public class XmlUtil { #region GetAttrib(XmlNode node, string attrib, string defVal) public static string GetAttrib(XmlNode node, string attrib, string defVal) { XmlAttribute xmlAttrib = node.Attributes[attrib]; if (xmlAttrib == null) return defVal; string val = xmlAttrib.Value; return (val == null) ? defVal : val; } #endregion #region GetAttribOrThrow(XmlNode node, string attrib) public static string GetAttribOrThrow(XmlNode node, string attrib) { string val = GetAttrib(node, attrib, null); if (val == null) throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name)); return val; } #endregion } }
Set Attribute Value
using System; using System.Text; using System.Xml; //GNU General Public License version 2 (GPLv2) //http://cbasetest.codeplex.com/license class MainClass{ public static void SetAttributeValue(XmlNode xmlne, string name, object value) { SetAttributeValue(xmlne, name, value, null); } public static void SetAttributeValue(XmlNode xmlne, string name, string value) { XmlAttribute node = xmlne.OwnerDocument.CreateAttribute(name); xmlne.Attributes.Append(node); node.Value = value; } public static void SetAttributeValue(XmlNode xmlne, string name, object value, string defaultStr) { string str = defaultStr; if (value != null) { str = value.ToString(); } if (str != null) { SetAttributeValue(xmlne, name, str); } } }