17 using System.Globalization;
20 using erminas.SmartAPI.CMS;
21 using erminas.SmartAPI.Exceptions;
23 namespace erminas.SmartAPI.Utils
25 public static class XmlUtil
36 public static void AddAttribute(
this XmlElement xmlElement,
string attributeName,
string value)
38 XmlAttribute attr = xmlElement.OwnerDocument.CreateAttribute(attributeName);
40 xmlElement.Attributes.Append(attr);
49 public static XmlElement AddElement(
this XmlNode node,
string name)
51 var doc = node as XmlDocument;
52 doc = doc ?? node.OwnerDocument;
53 XmlElement element = doc.CreateElement(name);
54 node.AppendChild(element);
64 public static string GetAttributeValue(
this XmlElement xmlElement,
string attributeName)
66 XmlAttribute attr = xmlElement.Attributes[attributeName];
67 return attr == null ? null : attr.Value;
70 public static bool? GetBoolAttributeValue(
this XmlElement xmlElement,
string attributeName)
72 int? value = xmlElement.GetIntAttributeValue(attributeName);
87 "Could not convert value '{0}' of attribute '{1}' to a boolean value", value,
91 public static double? GetDoubleAttributeValue(
this XmlElement xmlElement,
string attributeName)
93 XmlAttribute attr = xmlElement.Attributes[attributeName];
94 return attr == null ? (
double?) null : Double.Parse(attr.Value, CultureInfo.InvariantCulture);
97 public static Guid GetGuid(
this XmlElement xmlElement)
99 return xmlElement.GetGuid(
"guid");
102 public static Guid GetGuid(
this XmlElement xmlElement, String attributeName)
104 return Guid.Parse(xmlElement.GetAttributeValue(attributeName));
107 public static int? GetIntAttributeValue(
this XmlElement xmlElement,
string attributeName)
109 XmlAttribute attr = xmlElement.Attributes[attributeName];
110 return attr == null ? (
int?) null :
int.Parse(attr.Value);
113 public static string GetName(
this XmlElement xmlElement)
115 return xmlElement.GetAttributeValue(
"name");
118 public static DateTime? GetOADate(
this XmlElement element,
string attributeName =
"date")
120 string strValue = element.GetAttributeValue(attributeName);
121 if (String.IsNullOrEmpty(strValue))
126 return strValue.ToOADate();
129 public static XmlElement GetSingleElement(
this XmlDocument doc,
string tagName)
131 var nodes = doc.GetElementsByTagName(tagName);
132 if (nodes.Count != 1)
135 string.Format(
"Invalid number of {0} elements in XML reply from server. Expected: 1 actual: {1}",
136 tagName, nodes.Count));
139 return (XmlElement) nodes[0];
142 public static bool IsAttributeSet(
this XmlElement xmlElement,
ISessionObject session,
string attributeName)
144 var strValue = xmlElement.GetAttributeValue(attributeName);
146 return !
string.IsNullOrEmpty(strValue) && strValue != session.
Session.
SessionKey;
149 public static bool IsContainingOk(
this XmlDocument xmlDoc)
151 return xmlDoc.InnerText.Contains(
"ok");
161 public static string NodeToString(
this XmlElement xmlElement)
163 var sw =
new StringWriter();
164 var xw =
new XmlTextWriter(sw);
165 xmlElement.WriteTo(xw);
167 return sw.ToString();
176 public static void SetAttributeValue(
this XmlElement xmlElement,
string attributeName,
string value)
178 XmlAttribute attr = xmlElement.Attributes[attributeName];
181 AddAttribute(xmlElement, attributeName, value);
189 public static DateTime ToOADate(
this string value)
191 string valueNormalizedToInvariantCulture = value.Replace(
",",
".");
192 return DateTime.FromOADate(Double.Parse(valueNormalizedToInvariantCulture, CultureInfo.InvariantCulture));
195 public static bool TryGetGuid(
this XmlElement xmlElement, out Guid guid)
197 return TryGetGuid(xmlElement,
"guid", out guid);
200 public static bool TryGetGuid(
this XmlElement xmlElement,
string attributeName, out Guid guid)
202 string strValue = xmlElement.GetAttributeValue(attributeName);
203 if (
string.IsNullOrEmpty(strValue))
209 return Guid.TryParse(strValue, out guid);