mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
Removing Utils directory to better match namespaces
This commit is contained in:
75
ICD.Common.Utils/Xml/IcdXmlAttribute.cs
Normal file
75
ICD.Common.Utils/Xml/IcdXmlAttribute.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
/// <summary>
|
||||
/// IcdXmlAttribute represents an attribute="value" pair from xml.
|
||||
/// </summary>
|
||||
public struct IcdXmlAttribute
|
||||
{
|
||||
private readonly string m_Name;
|
||||
private readonly string m_Value;
|
||||
|
||||
public string Name { get { return m_Name; } }
|
||||
public string Value { get { return m_Value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="value"></param>
|
||||
public IcdXmlAttribute(string name, string value)
|
||||
{
|
||||
m_Name = name;
|
||||
m_Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementing default equality.
|
||||
/// </summary>
|
||||
/// <param name="a1"></param>
|
||||
/// <param name="a2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator ==(IcdXmlAttribute a1, IcdXmlAttribute a2)
|
||||
{
|
||||
return a1.Equals(a2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementing default inequality.
|
||||
/// </summary>
|
||||
/// <param name="a1"></param>
|
||||
/// <param name="a2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator !=(IcdXmlAttribute a1, IcdXmlAttribute a2)
|
||||
{
|
||||
return !(a1 == a2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this instance is equal to the given object.
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (other == null || GetType() != other.GetType())
|
||||
return false;
|
||||
|
||||
return GetHashCode() == ((IcdXmlAttribute)other).GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hashcode for this instance.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
int hash = 17;
|
||||
hash = hash * 23 + (m_Name == null ? 0 : m_Name.GetHashCode());
|
||||
hash = hash * 23 + (m_Value == null ? 0 : m_Value.GetHashCode());
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
ICD.Common.Utils/Xml/IcdXmlConvert.cs
Normal file
101
ICD.Common.Utils/Xml/IcdXmlConvert.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
#else
|
||||
using System.Xml;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public static class IcdXmlConvert
|
||||
{
|
||||
public static string ToString(int value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(bool value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(float value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(double value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(decimal value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(long value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(ulong value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(Guid value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(TimeSpan value)
|
||||
{
|
||||
return XmlConvert.ToString(value);
|
||||
}
|
||||
|
||||
public static string ToString(int? value)
|
||||
{
|
||||
return value.HasValue ? ToString(value.Value) : null;
|
||||
}
|
||||
|
||||
public static string ToString(object child)
|
||||
{
|
||||
if (child == null)
|
||||
return null;
|
||||
|
||||
if (child is bool)
|
||||
return ToString((bool)child);
|
||||
if (child is byte)
|
||||
return ToString((byte)child);
|
||||
if (child is decimal)
|
||||
return ToString((decimal)child);
|
||||
if (child is char)
|
||||
return ToString((char)child);
|
||||
if (child is double)
|
||||
return ToString((double)child);
|
||||
if (child is Guid)
|
||||
return ToString((Guid)child);
|
||||
if (child is float)
|
||||
return ToString((float)child);
|
||||
if (child is int)
|
||||
return ToString((int)child);
|
||||
if (child is long)
|
||||
return ToString((long)child);
|
||||
if (child is sbyte)
|
||||
return ToString((sbyte)child);
|
||||
if (child is short)
|
||||
return ToString((short)child);
|
||||
if (child is TimeSpan)
|
||||
return ToString((TimeSpan)child);
|
||||
if (child is uint)
|
||||
return ToString((uint)child);
|
||||
if (child is ulong)
|
||||
return ToString((ulong)child);
|
||||
if (child is ushort)
|
||||
return ToString((ushort)child);
|
||||
|
||||
return child.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
40
ICD.Common.Utils/Xml/IcdXmlDocument.cs
Normal file
40
ICD.Common.Utils/Xml/IcdXmlDocument.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
|
||||
#else
|
||||
using System.Xml;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed class IcdXmlDocument
|
||||
{
|
||||
private readonly XmlDocument m_Document;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public IcdXmlDocument()
|
||||
{
|
||||
m_Document = new XmlDocument();
|
||||
}
|
||||
|
||||
public void LoadXml(string xml)
|
||||
{
|
||||
try
|
||||
{
|
||||
m_Document.LoadXml(xml);
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteContentTo(IcdXmlTextWriter writer)
|
||||
{
|
||||
m_Document.WriteContentTo(writer.WrappedWriter);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
ICD.Common.Utils/Xml/IcdXmlException.cs
Normal file
24
ICD.Common.Utils/Xml/IcdXmlException.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed class IcdXmlException : Exception
|
||||
{
|
||||
private int m_LineNumber;
|
||||
private int m_LinePosition;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="inner"></param>
|
||||
/// <param name="lineNumber"></param>
|
||||
/// <param name="linePosition"></param>
|
||||
public IcdXmlException(string message, Exception inner, int lineNumber, int linePosition)
|
||||
: base(message, inner)
|
||||
{
|
||||
m_LineNumber = lineNumber;
|
||||
m_LinePosition = linePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
128
ICD.Common.Utils/Xml/IcdXmlReader.cs
Normal file
128
ICD.Common.Utils/Xml/IcdXmlReader.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
#else
|
||||
using System.Xml;
|
||||
using System.IO;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed class IcdXmlReader : IDisposable
|
||||
{
|
||||
private readonly XmlReader m_Reader;
|
||||
|
||||
#region Properties
|
||||
|
||||
public bool HasAttributes { get { return m_Reader.HasAttributes; } }
|
||||
|
||||
public string Name { get { return m_Reader.Name; } }
|
||||
|
||||
public string Value { get { return m_Reader.Value; } }
|
||||
|
||||
public XmlNodeType NodeType { get { return m_Reader.NodeType; } }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="xml"></param>
|
||||
public IcdXmlReader(string xml)
|
||||
{
|
||||
if (xml == null)
|
||||
throw new ArgumentNullException("xml");
|
||||
|
||||
#if SIMPLSHARP
|
||||
m_Reader = new XmlReader(xml);
|
||||
#else
|
||||
m_Reader = XmlReader.Create(new StringReader(xml));
|
||||
#endif
|
||||
}
|
||||
|
||||
~IcdXmlReader()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
public bool MoveToNextAttribute()
|
||||
{
|
||||
return m_Reader.MoveToNextAttribute();
|
||||
}
|
||||
|
||||
public void MoveToElement()
|
||||
{
|
||||
m_Reader.MoveToElement();
|
||||
}
|
||||
|
||||
public string GetAttribute(string name)
|
||||
{
|
||||
return m_Reader.GetAttribute(name);
|
||||
}
|
||||
|
||||
public string ReadString()
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
return m_Reader.ReadString();
|
||||
#else
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
#endif
|
||||
}
|
||||
|
||||
public bool Read()
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_Reader.Read();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
m_Reader.Dispose(true);
|
||||
#else
|
||||
m_Reader.Dispose();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Skip()
|
||||
{
|
||||
m_Reader.Skip();
|
||||
}
|
||||
|
||||
public string ReadElementContentAsString()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
}
|
||||
|
||||
public string ReadOuterXml()
|
||||
{
|
||||
return m_Reader.ReadOuterXml();
|
||||
}
|
||||
|
||||
public string ReadInnerXml()
|
||||
{
|
||||
return m_Reader.ReadInnerXml();
|
||||
}
|
||||
|
||||
public long ReadElementContentAsLong()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsLong();
|
||||
}
|
||||
|
||||
public float ReadElementContentAsFloat()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsFloat();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
94
ICD.Common.Utils/Xml/IcdXmlTextWriter.SimplSharp.cs
Normal file
94
ICD.Common.Utils/Xml/IcdXmlTextWriter.SimplSharp.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
#if SIMPLSHARP
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
using ICD.Common.Utils.IO;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed partial class IcdXmlTextWriter
|
||||
{
|
||||
private readonly XmlTextWriter m_Writer;
|
||||
|
||||
public XmlWriter WrappedWriter { get { return m_Writer; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="encoding"></param>
|
||||
public IcdXmlTextWriter(IcdStream stream, Encoding encoding)
|
||||
: this(new XmlTextWriter(stream.WrappedStream, encoding))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="textWriter"></param>
|
||||
public IcdXmlTextWriter(IcdTextWriter textWriter)
|
||||
: this(new XmlTextWriter(textWriter.WrappedTextWriter))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
public IcdXmlTextWriter(XmlTextWriter writer)
|
||||
{
|
||||
m_Writer = writer;
|
||||
m_Writer.Formatting = Formatting.Indented;
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
public void WriteStartElement(string elementName)
|
||||
{
|
||||
m_Writer.WriteStartElement(elementName);
|
||||
}
|
||||
|
||||
public void WriteElementString(string elementName, string value)
|
||||
{
|
||||
m_Writer.WriteElementString(elementName, value);
|
||||
}
|
||||
|
||||
public void WriteEndElement()
|
||||
{
|
||||
m_Writer.WriteEndElement();
|
||||
}
|
||||
|
||||
public void WriteComment(string comment)
|
||||
{
|
||||
m_Writer.WriteComment(comment);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_Writer.Dispose(true);
|
||||
}
|
||||
|
||||
public void WriteAttributeString(string attributeName, string value)
|
||||
{
|
||||
m_Writer.WriteAttributeString(attributeName, value);
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
m_Writer.Flush();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
m_Writer.Close();
|
||||
}
|
||||
|
||||
public void WriteRaw(string xml)
|
||||
{
|
||||
m_Writer.WriteRaw(xml);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
102
ICD.Common.Utils/Xml/IcdXmlTextWriter.Standard.cs
Normal file
102
ICD.Common.Utils/Xml/IcdXmlTextWriter.Standard.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using ICD.Common.Utils.IO;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed partial class IcdXmlTextWriter
|
||||
{
|
||||
private readonly XmlWriter m_Writer;
|
||||
|
||||
public XmlWriter WrappedWriter { get { return m_Writer; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="encoding"></param>
|
||||
public IcdXmlTextWriter(IcdStream stream, Encoding encoding)
|
||||
: this(XmlWriter.Create(stream.WrappedStream, GetSettings(encoding)))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="textWriter"></param>
|
||||
public IcdXmlTextWriter(IcdTextWriter textWriter)
|
||||
: this(XmlWriter.Create(textWriter.WrappedTextWriter))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
public IcdXmlTextWriter(XmlWriter writer)
|
||||
{
|
||||
m_Writer = writer;
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
public void WriteStartElement(string elementName)
|
||||
{
|
||||
m_Writer.WriteStartElement(elementName);
|
||||
}
|
||||
|
||||
public void WriteElementString(string elementName, string value)
|
||||
{
|
||||
m_Writer.WriteElementString(elementName, value);
|
||||
}
|
||||
|
||||
public void WriteEndElement()
|
||||
{
|
||||
m_Writer.WriteEndElement();
|
||||
}
|
||||
|
||||
public void WriteComment(string comment)
|
||||
{
|
||||
m_Writer.WriteComment(comment);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_Writer.Dispose();
|
||||
}
|
||||
|
||||
public void WriteAttributeString(string attributeName, string value)
|
||||
{
|
||||
m_Writer.WriteAttributeString(attributeName, value);
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
m_Writer.Flush();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
}
|
||||
|
||||
public void WriteRaw(string xml)
|
||||
{
|
||||
m_Writer.WriteRaw(xml);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static XmlWriterSettings GetSettings(Encoding encoding)
|
||||
{
|
||||
return new XmlWriterSettings
|
||||
{
|
||||
Encoding = encoding,
|
||||
Indent = true
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
8
ICD.Common.Utils/Xml/IcdXmlTextWriter.cs
Normal file
8
ICD.Common.Utils/Xml/IcdXmlTextWriter.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed partial class IcdXmlTextWriter : IDisposable
|
||||
{
|
||||
}
|
||||
}
|
||||
315
ICD.Common.Utils/Xml/XmlReaderExtensions.cs
Normal file
315
ICD.Common.Utils/Xml/XmlReaderExtensions.cs
Normal file
@@ -0,0 +1,315 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.CrestronXml;
|
||||
#else
|
||||
using System.Xml;
|
||||
#endif
|
||||
using ICD.Common.EventArguments;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public static class XmlReaderExtensions
|
||||
{
|
||||
#region Attributes
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the attribute exists.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool HasAttribute(this IcdXmlReader extends, string name)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetAttribute(name) != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the attributes for the current element without moving the reader.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<IcdXmlAttribute> GetAttributes(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (!extends.HasAttributes)
|
||||
return new IcdXmlAttribute[0];
|
||||
|
||||
List<IcdXmlAttribute> attributes = new List<IcdXmlAttribute>();
|
||||
while (extends.MoveToNextAttribute())
|
||||
attributes.Add(new IcdXmlAttribute(extends.Name, extends.Value));
|
||||
|
||||
// Move back to element.
|
||||
extends.MoveToElement();
|
||||
|
||||
return attributes.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the attribute with the given name.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string GetAttributeAsString(this IcdXmlReader extends, string name)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string output = extends.GetAttribute(name);
|
||||
if (output == null)
|
||||
throw new FormatException(string.Format("Missing attribute \"{0}\"", name));
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the attribute with the given name and returns as an integer.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static int GetAttributeAsInt(this IcdXmlReader extends, string name)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string value = extends.GetAttributeAsString(name);
|
||||
return int.Parse(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the attribute with the given name and returns as a bool.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool GetAttributeAsBool(this IcdXmlReader extends, string name)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string value = extends.GetAttributeAsString(name);
|
||||
return bool.Parse(value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Recurse
|
||||
|
||||
/// <summary>
|
||||
/// Recurses through the entire XML, calling the callback for each Element/Text node.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="callback"></param>
|
||||
[PublicAPI]
|
||||
public static void Recurse(this IcdXmlReader extends, Action<XmlRecursionEventArgs> callback)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
XmlUtils.Recurse(extends.ReadString(), callback);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skip
|
||||
|
||||
/// <summary>
|
||||
/// Skips over insignificant whitespace.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
[PublicAPI]
|
||||
public static void SkipInsignificantWhitespace(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
while (extends.NodeType == XmlNodeType.Whitespace && extends.Read())
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skips the current node to the next element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
[PublicAPI]
|
||||
public static bool SkipToNextElement(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
while (extends.Read() && extends.NodeType != XmlNodeType.Element)
|
||||
{
|
||||
}
|
||||
|
||||
return extends.NodeType == XmlNodeType.Element;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get Child Element
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the current node has child elements.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool HasChildElements(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
while (extends.Read() && extends.NodeType != XmlNodeType.EndElement)
|
||||
{
|
||||
if (extends.NodeType == XmlNodeType.Element)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<IcdXmlReader> GetChildElements(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
foreach (IcdXmlReader output in extends.GetChildElementsAsString().Select(child => new IcdXmlReader(child)))
|
||||
{
|
||||
output.SkipToNextElement();
|
||||
yield return output;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child elements for the current element.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<string> GetChildElementsAsString(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// Step into the first child.
|
||||
extends.SkipToNextElement();
|
||||
|
||||
while (extends.NodeType == XmlNodeType.Element || extends.NodeType == XmlNodeType.Comment)
|
||||
{
|
||||
switch (extends.NodeType)
|
||||
{
|
||||
case XmlNodeType.Comment:
|
||||
extends.Skip();
|
||||
break;
|
||||
|
||||
case XmlNodeType.Element:
|
||||
yield return extends.ReadOuterXml();
|
||||
break;
|
||||
}
|
||||
|
||||
extends.SkipInsignificantWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read Element Content
|
||||
|
||||
/// <summary>
|
||||
/// Parses the element content in the format 0xXX as a byte.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static byte ReadElementContentAsByte(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string content = extends.ReadElementContentAsString();
|
||||
return (byte)Convert.ToInt64(content, 16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the element content as a uint.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static uint ReadElementContentAsUint(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string content = extends.ReadElementContentAsString();
|
||||
return uint.Parse(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the element content as a uint.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static int ReadElementContentAsInt(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string content = extends.ReadElementContentAsString();
|
||||
return int.Parse(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the element content as a ushort.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static ushort ReadElementContentAsUShort(this IcdXmlReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string content = extends.ReadElementContentAsString();
|
||||
return ushort.Parse(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the element content as an enum.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="ignoreCase"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static T ReadElementContentAsEnum<T>(this IcdXmlReader extends, bool ignoreCase)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
string content = extends.ReadElementContentAsString();
|
||||
return EnumUtils.Parse<T>(content, ignoreCase);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
1025
ICD.Common.Utils/Xml/XmlUtils.cs
Normal file
1025
ICD.Common.Utils/Xml/XmlUtils.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user