using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using System.Xml.Linq; namespace Sarand.Net.Standard.Base.Utils { //======================================================================= public static class SacsXmlUtil { //------------------------------------------------------------------- // Data members. public static Encoding DefaultEncoding { get; } = Encoding.UTF8; //------------------------------------------------------------------- /// Retrieve the Xml Declaration from the XML document. public static XDeclaration GetXmlDeclaration (this XDocument xmlDoc) { return xmlDoc.Declaration; } //------------------------------------------------------------------- // Namespace. public static String GetNamespacePrefix (this XElement element) { if (element.Name.Namespace != null) return element.GetPrefixOfNamespace(element.Name.Namespace); return ""; } //------------------------------------------------------------------- // Convert XML document to string. public static String GetXml (this XContainer container, Encoding encoding = null) { //return container.ToString(SaveOptions.DisableFormatting); var builder = new StringBuilder(); var settings = new XmlWriterSettings() { Indent = false }; using (var writer = XmlWriter.Create(builder, settings)) { container.WriteTo(writer); } return builder.ToString(); } public static String FormatXml (this XContainer container, Encoding encoding = null) { //return container.ToString(); var builder = new StringBuilder(); var settings = new XmlWriterSettings() { Indent = true }; using (var writer = XmlWriter.Create(builder, settings)) { container.WriteTo(writer); } return builder.ToString(); } //------------------------------------------------------------------- // N o d e N a m e //------------------------------------------------------------------- public static String GetLocalName (this XElement element) { return element?.Name?.LocalName ?? ""; } //------------------------------------------------------------------- // A t t r i b u t e s //------------------------------------------------------------------- public static XAttribute GetAttribute (this XElement element, String name) { foreach (XAttribute attribute in element.Attributes()) if (attribute.Name.LocalName.EqualsCI(name)) return attribute; return null; } public static bool HasAttribute (this XElement element, String name) { return element.GetAttribute(name) != null; } public static String GetAttributeValue (this XElement element, String name) { XAttribute attribute = element.GetAttribute(name); if (attribute != null) return attribute.Value; return ""; } public static void SetAttribute (this XElement element, String name, String value) { element.SetAttributeValue(name, value); } //------------------------------------------------------------------- // C h i l d N o d e s //------------------------------------------------------------------- public static List GetChildElements (this XContainer container, String name = "") { List childElements = new List(); if (container == null) return childElements; foreach (XElement element in container.Elements()) { if (StringUtil.IsEmpty(name) || element.Name.LocalName.EqualsCI(name)) childElements.Add(new XElement(element)); } return childElements; } public static bool HasChildNode (this XContainer container, String name) { if (container == null) return false; foreach (XElement element in container.Elements()) { if (StringUtil.IsEmpty(name) || element.Name.LocalName.EqualsCI(name)) return true; } return false; } public static XElement GetFirstChildElement (this XContainer container, String name = "") { if (container == null) return null; foreach (XElement element in container.Elements()) { if (StringUtil.IsEmpty(name) || element.Name.LocalName.EqualsCI(name)) return element; } return null; } public static XElement GetNextSiblingElement (this XElement child, String name = "") { if (child == null) return null; XNode sibling = null; while ((sibling = child.NextNode) != null) { if (sibling is XElement && (StringUtil.IsEmpty(name) || (sibling as XElement).Name.LocalName.EqualsCI(name))) return sibling as XElement; } return null; } //------------------------------------------------------------------- // System.Xml.Linq variation. /// Get text from first child that matches the node name. public static String GetChildText (this XElement element, String name) { foreach (XElement child in element.Elements()) if (child.Name.LocalName.EqualsCI(name)) return child.Value; return String.Empty; } // System.Xml varations. public static String GetChildText(this XmlNode node, String name) { foreach (XmlNode child in node.ChildNodes) if (child.Name.EqualsCI(name)) return child.InnerText; return String.Empty; } //------------------------------------------------------------------- // Append element to node. // System.Xml.Linq variation. public static XElement AppendElement (this XContainer container, XElement element) { container.Add(element); return element; } public static XElement AppendElement (this XContainer container, String name, String text = "") { XElement child = new XElement(name, text); container.Add(child); return child; } public static XElement AppendElement (this XContainer container, XNamespace xmlNamespace, String name, String text = "") { XElement child = new XElement(xmlNamespace + name, text); container.Add(child); return child; } // System.Xml varations. public static XmlElement AppendElement(this XmlNode node, String name, String text = "") { XmlDocument doc = (node is XmlDocument ? node as XmlDocument : node.OwnerDocument); XmlElement child = (XmlElement)node.AppendChild(doc.CreateElement(name)); if (!StringUtil.IsEmpty(text)) child.AppendChild(doc.CreateTextNode(text)); return child; } //------------------------------------------------------------------- } //======================================================================= }