Files
Common-VS2019/SacsStandardData/Data/Xml/SaMsXmlUtil.cs
T

126 lines
4.6 KiB
C#

using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Sarand.Net.Standard.Data.Xml
{
//=======================================================================
public class SaXmlNamespace
{
public SaXmlNamespace (String prefix, String uri)
{
Prefix = prefix;
Uri = uri;
}
public String Prefix { get; }
public String Uri { get; }
}
//=======================================================================
public static class SaMsXmlUtil
{
//-------------------------------------------------------------------
// Data members.
public static Encoding DefaultEncoding { get; } = Encoding.UTF8;
//-------------------------------------------------------------------
/// <summary>Retrieve the Xml Declaration from the XML document.</summary>
public static XmlDeclaration GetXmlDeclaration (this XmlDocument xmlDoc)
{
return xmlDoc.ChildNodes.OfType<XmlDeclaration>().FirstOrDefault();
}
//-------------------------------------------------------------------
// Convert XML document to string.
public static String GetXml (this XmlDocument xmlDoc, Encoding encoding = null)
{
// Ensure correct encoding for generated XML data.
if (encoding == null) encoding = DefaultEncoding;
XmlWriterSettings settings = new XmlWriterSettings() { Encoding = encoding };
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter, settings))
{
// Ensure that the document has the requested encoding in its XML declaration.
XmlDeclaration xmlDeclaration = xmlDoc.GetXmlDeclaration();
if (xmlDeclaration == null)
{
xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", encoding.WebName, null);
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
}
else xmlDeclaration.Encoding = encoding.WebName;
// Write the document to String.
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return stringWriter.GetStringBuilder().ToString();
}
}
public static String FormatXml (this XmlDocument xmlDoc, Encoding encoding = null)
{
// Ensure correct encoding for generated XML data.
if (encoding == null) encoding = DefaultEncoding;
XmlWriterSettings settings = new XmlWriterSettings() { Encoding = encoding, Indent = true };
using (MemoryStream memoryStream = new MemoryStream())
using (var xmlTextWriter = new XmlTextWriter(memoryStream, encoding))
{
// XML output format.
xmlTextWriter.Formatting = Formatting.Indented;
// Ensure that the document has the requested encoding in its XML declaration.
XmlDeclaration xmlDeclaration = xmlDoc.GetXmlDeclaration();
if (xmlDeclaration == null)
{
xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", encoding.WebName, null);
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
}
else xmlDeclaration.Encoding = encoding.WebName;
// Write the document to String.
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
memoryStream.Flush();
memoryStream.Position = 0;
using (StreamReader streamReader = new StreamReader(memoryStream))
{
return streamReader.ReadToEnd();
}
}
}
//-------------------------------------------------------------------
// Append element to node.
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;
}
public static XmlElement AppendElement (this XmlNode node, SaXmlNamespace xmlNamespace, String name, String text = "")
{
XmlDocument doc = (node is XmlDocument ? node as XmlDocument : node.OwnerDocument);
XmlElement child = (XmlElement)node.AppendChild(doc.CreateElement(xmlNamespace.Prefix, name, xmlNamespace.Uri));
if (!StringUtil.IsEmpty(text))
child.AppendChild(doc.CreateTextNode(text));
return child;
}
//-------------------------------------------------------------------
// Get child text.
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;
}
//-------------------------------------------------------------------
}
//=======================================================================
}