using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Text;
using System.IO;
using EasyDevCore.Common;
#pragma warning disable CS8601 // Possible null reference assignment.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8603 // Possible null reference return.
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
namespace EasyDevCore.Common.Wrapper
{
///
///
///
public class XMLWrapper: IDisposable
{
///
/// Gets the text.
///
///
/// The text.
///
public string Text { get; private set; }
///
/// The m_ disposed
///
private bool m_Disposed = false;
///
/// The m_ reader
///
private StringReader m_Reader = null;
///
/// Gets the X doc.
///
///
/// The X doc.
///
public XDocument XDoc { get; private set; }
///
/// Creates the intance.
///
/// The XML.
///
public XMLWrapper CreateIntance(string xml)
{
return new XMLWrapper(xml);
}
///
/// Initializes a new instance of the class.
///
/// The XML.
public XMLWrapper(string xml)
{
Text = xml;
m_Reader = new StringReader(xml);
XDoc = XDocument.Load(m_Reader);
}
///
/// Gets the elements.
///
/// The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).
/// The descendant element.
///
public IEnumerable GetElements(string xPathExpression, XElement descendantElement = null)
{
if(descendantElement != null) descendantElement.XPathSelectElements(xPathExpression);
return XDoc.XPathSelectElements(xPathExpression);
}
///
/// Gets the element.
///
/// The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).
/// The descendant element.
///
public XElement GetElement(string xPathExpression, XElement descendantElement = null)
{
if (descendantElement != null) descendantElement.XPathSelectElement(xPathExpression);
return XDoc.XPathSelectElement(xPathExpression);
}
///
/// Gets the element XML.
///
/// The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).
/// The descendant element.
///
public IEnumerable GetElementsXML(string xPathExpression, XElement descendantElement = null)
{
return GetElements(xPathExpression, descendantElement).Select(e => e.ToString());
}
///
/// Gets the element XML.
///
/// The x path expression ("./Customer/CompanyName" ~ Element("Customer").Elements("CompanyName")).
/// The descendant element.
///
public string GetElementXML(string xPathExpression, XElement descendantElement = null)
{
return GetElement(xPathExpression, descendantElement).ToString();
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool disposing)
{
if (!this.m_Disposed)
{
if (disposing)
{
m_Reader.Close();
m_Reader = null;
XDoc = null;
}
m_Disposed = true;
}
}
}
}