SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
XmlUtil.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Globalization;
18 using System.IO;
19 using System.Xml;
20 using erminas.SmartAPI.CMS;
21 using erminas.SmartAPI.Exceptions;
22 
23 namespace erminas.SmartAPI.Utils
24 {
25  public static class XmlUtil
26  {
36  public static void AddAttribute(this XmlElement xmlElement, string attributeName, string value)
37  {
38  XmlAttribute attr = xmlElement.OwnerDocument.CreateAttribute(attributeName);
39  attr.Value = value;
40  xmlElement.Attributes.Append(attr);
41  }
42 
49  public static XmlElement AddElement(this XmlNode node, string name)
50  {
51  var doc = node as XmlDocument;
52  doc = doc ?? node.OwnerDocument;
53  XmlElement element = doc.CreateElement(name);
54  node.AppendChild(element);
55  return element;
56  }
57 
64  public static string GetAttributeValue(this XmlElement xmlElement, string attributeName)
65  {
66  XmlAttribute attr = xmlElement.Attributes[attributeName];
67  return attr == null ? null : attr.Value;
68  }
69 
70  public static bool? GetBoolAttributeValue(this XmlElement xmlElement, string attributeName)
71  {
72  int? value = xmlElement.GetIntAttributeValue(attributeName);
73  if (value == null)
74  {
75  return null;
76  }
77  if (value == 1)
78  {
79  return true;
80  }
81  if (value == 0)
82  {
83  return false;
84  }
85  throw new SmartAPIException((ServerLogin) null,
86  string.Format(
87  "Could not convert value '{0}' of attribute '{1}' to a boolean value", value,
88  attributeName));
89  }
90 
91  public static double? GetDoubleAttributeValue(this XmlElement xmlElement, string attributeName)
92  {
93  XmlAttribute attr = xmlElement.Attributes[attributeName];
94  return attr == null ? (double?) null : Double.Parse(attr.Value, CultureInfo.InvariantCulture);
95  }
96 
97  public static Guid GetGuid(this XmlElement xmlElement)
98  {
99  return xmlElement.GetGuid("guid");
100  }
101 
102  public static Guid GetGuid(this XmlElement xmlElement, String attributeName)
103  {
104  return Guid.Parse(xmlElement.GetAttributeValue(attributeName));
105  }
106 
107  public static int? GetIntAttributeValue(this XmlElement xmlElement, string attributeName)
108  {
109  XmlAttribute attr = xmlElement.Attributes[attributeName];
110  return attr == null ? (int?) null : int.Parse(attr.Value);
111  }
112 
113  public static string GetName(this XmlElement xmlElement)
114  {
115  return xmlElement.GetAttributeValue("name");
116  }
117 
118  public static DateTime? GetOADate(this XmlElement element, string attributeName = "date")
119  {
120  string strValue = element.GetAttributeValue(attributeName);
121  if (String.IsNullOrEmpty(strValue))
122  {
123  return null;
124  }
125 
126  return strValue.ToOADate();
127  }
128 
129  public static XmlElement GetSingleElement(this XmlDocument doc, string tagName)
130  {
131  var nodes = doc.GetElementsByTagName(tagName);
132  if (nodes.Count != 1)
133  {
134  throw new SmartAPIInternalException(
135  string.Format("Invalid number of {0} elements in XML reply from server. Expected: 1 actual: {1}",
136  tagName, nodes.Count));
137  }
138 
139  return (XmlElement) nodes[0];
140  }
141 
142  public static bool IsAttributeSet(this XmlElement xmlElement, ISessionObject session, string attributeName)
143  {
144  var strValue = xmlElement.GetAttributeValue(attributeName);
145 
146  return !string.IsNullOrEmpty(strValue) && strValue != session.Session.SessionKey;
147  }
148 
149  public static bool IsContainingOk(this XmlDocument xmlDoc)
150  {
151  return xmlDoc.InnerText.Contains("ok");
152  }
153 
161  public static string NodeToString(this XmlElement xmlElement)
162  {
163  var sw = new StringWriter();
164  var xw = new XmlTextWriter(sw);
165  xmlElement.WriteTo(xw);
166 
167  return sw.ToString();
168  }
169 
176  public static void SetAttributeValue(this XmlElement xmlElement, string attributeName, string value)
177  {
178  XmlAttribute attr = xmlElement.Attributes[attributeName];
179  if (attr == null)
180  {
181  AddAttribute(xmlElement, attributeName, value);
182  }
183  else
184  {
185  attr.Value = value;
186  }
187  }
188 
189  public static DateTime ToOADate(this string value)
190  {
191  string valueNormalizedToInvariantCulture = value.Replace(",", ".");
192  return DateTime.FromOADate(Double.Parse(valueNormalizedToInvariantCulture, CultureInfo.InvariantCulture));
193  }
194 
195  public static bool TryGetGuid(this XmlElement xmlElement, out Guid guid)
196  {
197  return TryGetGuid(xmlElement, "guid", out guid);
198  }
199 
200  public static bool TryGetGuid(this XmlElement xmlElement, string attributeName, out Guid guid)
201  {
202  string strValue = xmlElement.GetAttributeValue(attributeName);
203  if (string.IsNullOrEmpty(strValue))
204  {
205  guid = Guid.Empty;
206  return false;
207  }
208 
209  return Guid.TryParse(strValue, out guid);
210  }
211  }
212 }