179 lines
6.5 KiB
C#
179 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Proc.Xml
|
|
{
|
|
//=======================================================================
|
|
public class SaMsXmlElement : SaMsXmlNode
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
internal SaMsXmlElement (XmlElement element) :
|
|
base(element)
|
|
{
|
|
}
|
|
protected override void Dispose (bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
internal XmlElement Element { get { return (node as XmlElement); } }
|
|
public override bool Valid { get { return (Element != null); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// A t t r i b u t e s
|
|
//-------------------------------------------------------------------
|
|
public Dictionary<String, String> GetAttributes ()
|
|
{
|
|
Dictionary<String, String> attributes = new Dictionary<string, string>();
|
|
foreach (XmlAttribute attribute in Element.Attributes)
|
|
attributes[attribute.Name] = attribute.Value;
|
|
return attributes;
|
|
}
|
|
public bool HasAttribute (String name, bool caseInsensitive = false)
|
|
{
|
|
foreach (XmlAttribute attribute in Element.Attributes)
|
|
{
|
|
if ((!caseInsensitive && attribute.Name.Equals(name)) || (caseInsensitive && attribute.Name.EqualsCI(name)))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public String GetAttribute (String name, bool caseInsensitive = false)
|
|
{
|
|
foreach (XmlAttribute attribute in Element.Attributes)
|
|
{
|
|
if ((!caseInsensitive && attribute.Name.Equals(name)) || (caseInsensitive && attribute.Name.EqualsCI(name)))
|
|
return attribute.Value;
|
|
}
|
|
return "";
|
|
}
|
|
public void SetAttribute (String name, String value)
|
|
{
|
|
Element.SetAttribute(name, value);
|
|
}
|
|
public bool RemoveAttribute (String name, bool caseInsensitive = false)
|
|
{
|
|
if (!HasAttribute(name, caseInsensitive)) return false;
|
|
for (int attributeIdx =0; attributeIdx < Element.Attributes.Count; attributeIdx++)
|
|
{
|
|
XmlAttribute attribute = Element.Attributes[attributeIdx];
|
|
if ((!caseInsensitive && attribute.Name.Equals(name)) || (caseInsensitive && attribute.Name.EqualsCI(name)))
|
|
{
|
|
Element.Attributes.RemoveAt(attributeIdx);
|
|
attributeIdx--;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// C h i l d E l e m e n t s
|
|
//-------------------------------------------------------------------
|
|
public List<SaMsXmlElement> GetChildElements (String name = "")
|
|
{
|
|
List<SaMsXmlElement> childElements = new List<SaMsXmlElement>();
|
|
XmlElement element = Element;
|
|
if (element == null) return childElements;
|
|
foreach (XmlNode childNode in element.ChildNodes)
|
|
{
|
|
if (StringUtil.IsEmpty(name) || childNode.Name.EqualsCI(name))
|
|
childElements.Add(new SaMsXmlElement(childNode as XmlElement));
|
|
}
|
|
return childElements;
|
|
}
|
|
public bool HasChildElement (String name)
|
|
{
|
|
XmlElement element = Element;
|
|
if (element == null) return false;
|
|
foreach (XmlNode childNode in element.ChildNodes)
|
|
{
|
|
if (childNode.NodeType == XmlNodeType.Element && StringUtil.IsEmpty(name) || childNode.Name.EqualsCI(name))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public SaMsXmlElement GetFirstChildElement (String name = "")
|
|
{
|
|
XmlElement element = Element;
|
|
if (element == null) return null;
|
|
foreach (XmlNode childNode in element.ChildNodes)
|
|
{
|
|
if (childNode.NodeType == XmlNodeType.Element && StringUtil.IsEmpty(name) || childNode.Name.EqualsCI(name))
|
|
return new SaMsXmlElement(childNode as XmlElement);
|
|
}
|
|
return new SaMsXmlElement(null);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// A p p e n d E l e m e n t s
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Append element to node by name.</summary>
|
|
public SaMsXmlElement AppendElement (String name, String text = "")
|
|
{
|
|
XmlDocument doc = Element.OwnerDocument;
|
|
XmlElement child = (XmlElement)Element.AppendChild(doc.CreateElement(name));
|
|
if (!StringUtil.IsEmpty(text))
|
|
child.AppendChild(doc.CreateTextNode(text));
|
|
return new SaMsXmlElement(child);
|
|
}
|
|
public SaMsXmlElement AppendElement (SaXmlNamespace xmlNamespace, String name, String text = "")
|
|
{
|
|
XmlDocument doc = Element.OwnerDocument;
|
|
XmlElement child = (XmlElement)Element.AppendChild(doc.CreateElement(xmlNamespace.Prefix, name, xmlNamespace.Uri));
|
|
if (!StringUtil.IsEmpty(text))
|
|
child.AppendChild(doc.CreateTextNode(text));
|
|
return new SaMsXmlElement(child);
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Append element to node by name if value supplied.</summary>
|
|
public SaMsXmlElement AppendNonBlankElement (String name, String text)
|
|
{
|
|
if (StringUtil.IsEmpty(text)) return null;
|
|
XmlDocument doc = Element.OwnerDocument;
|
|
XmlElement child = (XmlElement)Element.AppendChild(doc.CreateElement(name));
|
|
if (!StringUtil.IsEmpty(text))
|
|
child.AppendChild(doc.CreateTextNode(text));
|
|
return new SaMsXmlElement(child);
|
|
}
|
|
public SaMsXmlElement AppendNonBlankElement (SaXmlNamespace xmlNamespace, String name, String text)
|
|
{
|
|
if (StringUtil.IsEmpty(text)) return null;
|
|
XmlDocument doc = Element.OwnerDocument;
|
|
XmlElement child = (XmlElement)Element.AppendChild(doc.CreateElement(xmlNamespace.Prefix, name, xmlNamespace.Uri));
|
|
if (!StringUtil.IsEmpty(text))
|
|
child.AppendChild(doc.CreateTextNode(text));
|
|
return new SaMsXmlElement(child);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Append element to node.</summary>
|
|
public SaMsXmlElement AppendElement (SaMsXmlElement element)
|
|
{
|
|
// Import the node into the document before appending.
|
|
XmlDocument doc = Element.OwnerDocument;
|
|
XmlNode appendNode = doc.ImportNode(element.Element, true);
|
|
XmlElement child = (XmlElement)Element.AppendChild(appendNode);
|
|
return new SaMsXmlElement(child);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|