122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
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.Data.Xml
|
|
{
|
|
//=======================================================================
|
|
public class SaMsXmlDocument : SaErrorStateItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private XmlDocument document = new XmlDocument();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaMsXmlDocument ()
|
|
{
|
|
}
|
|
protected override void Dispose (bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
document = null;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public bool Valid { get { return (document != null); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// O p e n
|
|
//-------------------------------------------------------------------
|
|
public bool CreateNew (String rootElementName = "")
|
|
{
|
|
ClearErrorState();
|
|
document = new XmlDocument();
|
|
if (!StringUtil.IsEmpty(rootElementName))
|
|
document.AppendElement(rootElementName);
|
|
return !HasErrorState();
|
|
}
|
|
public bool LoadXml (String xml)
|
|
{
|
|
ClearErrorState();
|
|
document = new XmlDocument();
|
|
try
|
|
{
|
|
document.LoadXml(xml);
|
|
}
|
|
catch (Exception exc) { SetErrorState(ExceptionUtil.GetErrorStateMessage(exc)); }
|
|
return !HasErrorState();
|
|
}
|
|
public bool LoadXml (Stream xml)
|
|
{
|
|
ClearErrorState();
|
|
document = new XmlDocument();
|
|
try
|
|
{
|
|
xml.Seek(0, SeekOrigin.Begin);
|
|
document.Load(xml);
|
|
}
|
|
catch (Exception exc) { SetErrorState(ExceptionUtil.GetErrorStateMessage(exc)); }
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// C o n v e r s i o n
|
|
//-------------------------------------------------------------------
|
|
public static Encoding DefaultEncoding { get; } = Encoding.UTF8;
|
|
public String GetXml (Encoding encoding = null)
|
|
{
|
|
return document.GetXml(encoding != null ? encoding : DefaultEncoding);
|
|
}
|
|
public String FormatXml (Encoding encoding = null)
|
|
{
|
|
return document.FormatXml(encoding != null ? encoding : DefaultEncoding);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// E l e m e n t s
|
|
//-------------------------------------------------------------------
|
|
public SaMsXmlElement GetRootElement ()
|
|
{
|
|
if (document != null) return new SaMsXmlElement(document.DocumentElement);
|
|
return null;
|
|
}
|
|
public SaMsXmlElement SetRootElement (String name)
|
|
{
|
|
// Clear existing root element.
|
|
if (document.DocumentElement != null)
|
|
document.DocumentElement.ParentNode.RemoveAll();
|
|
// Add the new root element.
|
|
return new SaMsXmlElement(document.AppendElement(name));
|
|
}
|
|
public SaMsXmlElement SetRootElement (SaXmlNamespace xmlNamespace, String name)
|
|
{
|
|
// Clear existing root element.
|
|
if (document.DocumentElement != null)
|
|
document.DocumentElement.ParentNode.RemoveAll();
|
|
// Add the new root element.
|
|
return new SaMsXmlElement(document.AppendElement(xmlNamespace, name));
|
|
}
|
|
public SaMsXmlElement SetRootElement (SaMsXmlElement rootNode)
|
|
{
|
|
// Clear existing root element.
|
|
if (document.DocumentElement != null)
|
|
document.DocumentElement.ParentNode.RemoveAll();
|
|
// Import the node into the document before appending.
|
|
XmlNode importNode = document.ImportNode(rootNode.Element, true);
|
|
document.AppendChild(importNode);
|
|
return rootNode;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|