648 lines
19 KiB
C#
648 lines
19 KiB
C#
|
|
using System;
|
|||
|
|
using System.Xml;
|
|||
|
|
using CYQ.Data.Cache;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using CYQ.Data.Table;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Xml
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Xml<6D><6C>XHtml<6D>Ļ<EFBFBD><C4BB><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public abstract class XHtmlBase : IDisposable
|
|||
|
|
{
|
|||
|
|
private Encoding _Encoding = Encoding.UTF8;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public Encoding Encoding
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Encoding;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Encoding = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>LoadData(MDataRow,pre)<29><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>)
|
|||
|
|
/// </summary>
|
|||
|
|
protected Dictionary<string, MDataRow> dicForAutoSetValue;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// xml<6D><6C><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
protected XmlDocument _XmlDocument;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ڲ<EFBFBD>XmlDocument<6E><74><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public XmlDocument XmlDoc
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _XmlDocument;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
protected XmlNamespaceManager xnm;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
protected CacheManage theCache;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Html<6D><6C><EFBFBD>ƿռ<C6BF>
|
|||
|
|
/// </summary>
|
|||
|
|
protected string htmlNameSpace = "http://www.w3.org/1999/xhtml";
|
|||
|
|
|
|||
|
|
internal string PreXml = "preXml";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ص<EFBFBD>Xml<6D>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string FileName
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _FileName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string _FileName = string.Empty;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// xml<6D><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key
|
|||
|
|
/// </summary>
|
|||
|
|
public string XmlCacheKey = string.Empty;
|
|||
|
|
|
|||
|
|
private bool _IsNoClone;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD>ʱXHtmlӦ<6C><D3A6>ֻ<EFBFBD><D6BB>ģʽ)
|
|||
|
|
/// </summary>
|
|||
|
|
public bool IsNoClone
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _IsNoClone;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_IsNoClone = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool _IsLoadFromCache;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ĵ<EFBFBD><C4B5>Ƿ<EFBFBD>ȡ<EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool IsLoadFromCache
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _IsLoadFromCache;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_IsLoadFromCache = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private double _CacheMinutes = 5;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public double CacheMinutes
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _CacheMinutes;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>XHtml<6D><6C><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public virtual string OutXml
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _XmlDocument.OuterXml;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ص<EFBFBD>Html<6D>Ƿ<EFBFBD><C7B7>Ѹı<D1B8>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool IsXHtmlChanged
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return !theCache.Contains(XmlCacheKey);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
///// <summary>
|
|||
|
|
///// <20><><EFBFBD><EFBFBD>Html<6D><6C><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ͽڵ㡣
|
|||
|
|
///// </summary>
|
|||
|
|
//private bool clearCommentOnLoad = false;
|
|||
|
|
public XHtmlBase()
|
|||
|
|
{
|
|||
|
|
//License.Check(DAL.DalCreate.XHtmlClient);
|
|||
|
|
_XmlDocument = new XmlDocument();
|
|||
|
|
theCache = CacheManage.LocalInstance;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected void LoadNameSpace(string nameSpace)
|
|||
|
|
{
|
|||
|
|
xnm = new XmlNamespaceManager(_XmlDocument.NameTable);
|
|||
|
|
xnm.AddNamespace(PreXml, nameSpace);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ӿ<EFBFBD><D3BE><EFBFBD>·<EFBFBD><C2B7><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ΪKeyֵ
|
|||
|
|
/// </summary>
|
|||
|
|
private string GenerateKey(string fileName)
|
|||
|
|
{
|
|||
|
|
_FileName = fileName;
|
|||
|
|
fileName = fileName.Replace(AppConfig.WebRootPath, "XHtmlBase_");
|
|||
|
|
fileName = fileName.Replace("/", "").Replace("\\", "");
|
|||
|
|
return fileName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>xml
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// docTypeHtml<6D><6C>һ<EFBFBD><D2BB><EFBFBD>ġ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
protected static string docTypeHtml = string.Empty;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>xml<6D>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="xml">xml<6D>ַ<EFBFBD><D6B7><EFBFBD></param>
|
|||
|
|
public void LoadXml(string xml)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (xnm != null)
|
|||
|
|
{
|
|||
|
|
if (xml.StartsWith("<!DOCTYPE html"))
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(docTypeHtml))
|
|||
|
|
{
|
|||
|
|
docTypeHtml = "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"" + AppConfig.XHtml.DtdUri + "\">";
|
|||
|
|
}
|
|||
|
|
xml = xml.Replace(xml.Substring(0, xml.IndexOf('>') + 1), docTypeHtml);
|
|||
|
|
}
|
|||
|
|
// xml = xml.Replace("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", AppConfig.XHtml.DtdUri);
|
|||
|
|
}
|
|||
|
|
xml = Filter(xml);
|
|||
|
|
_XmlDocument.LoadXml(xml);
|
|||
|
|
}
|
|||
|
|
catch (XmlException err)
|
|||
|
|
{
|
|||
|
|
throw new XmlException(err.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC><EFBFBD>Xml
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool Load(string fileName)
|
|||
|
|
{
|
|||
|
|
return Load(fileName, XmlCacheLevel.Lower);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>XML
|
|||
|
|
/// </summary>
|
|||
|
|
public bool Load(string fileName, XmlCacheLevel level)
|
|||
|
|
{
|
|||
|
|
return Load(fileName, level, false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Xml<6D>ļ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fileName"><3E>ļ<EFBFBD><C4BC><EFBFBD></param>
|
|||
|
|
/// <param name="level"><3E>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>漶<EFBFBD><E6BCB6></param>
|
|||
|
|
/// <param name="clearCommentNode"><3E><><EFBFBD>غ<EFBFBD><D8BA>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ͽڵ<CDBD></param>
|
|||
|
|
public bool Load(string fileName, XmlCacheLevel level, bool clearCommentNode)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
bool loadState = false;
|
|||
|
|
XmlCacheKey = GenerateKey(fileName);//<2F><>·<EFBFBD><C2B7><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ϊkey
|
|||
|
|
//2019-03-21 zyk ע<>͵<EFBFBD><CDB5>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>룬<EFBFBD><EBA3AC><EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷſ<D9B7> kk ׯԪ<D7AF><D4AA>
|
|||
|
|
//if (level != XmlCacheLevel.NoCache)
|
|||
|
|
//{
|
|||
|
|
// loadState = LoadFromCache(XmlCacheKey);//<2F><>Cache<68><65><EFBFBD><EFBFBD>Xml
|
|||
|
|
//}
|
|||
|
|
if (!loadState)//Cache<68><65><EFBFBD><EFBFBD>Xmlʧ<6C><CAA7>
|
|||
|
|
{
|
|||
|
|
_CacheMinutes = (double)level;
|
|||
|
|
loadState = LoadFromFile(fileName, clearCommentNode);//<2F><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Xml
|
|||
|
|
}
|
|||
|
|
return loadState;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ӻ<EFBFBD><D3BB><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>html
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public bool LoadFromCache(string key)
|
|||
|
|
{
|
|||
|
|
if (theCache.Contains(key))//<2F><><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ڶ<EFBFBD>Ӧֵ<D3A6><D6B5>key<65>Ķ<EFBFBD><C4B6><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (_IsNoClone)
|
|||
|
|
{
|
|||
|
|
_XmlDocument = theCache.Get(key) as XmlDocument;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_XmlDocument = GetCloneFrom(theCache.Get(key) as XmlDocument);
|
|||
|
|
}
|
|||
|
|
_IsLoadFromCache = true;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ͽڵ<CDBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public virtual void RemoveCommentNode()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>XML <20><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5>õķ<C3B5><C4B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private bool LoadFromFile(string fileName, bool clearCommentNode)
|
|||
|
|
{
|
|||
|
|
if (!System.IO.File.Exists(fileName))
|
|||
|
|
{
|
|||
|
|
//Log.WriteLog("filename no exist : " + fileName);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string html = string.Empty;
|
|||
|
|
if (xnm != null)
|
|||
|
|
{
|
|||
|
|
html = IOHelper.ReadAllText(fileName, 0, _Encoding);
|
|||
|
|
ResolverDtd.Resolver(ref _XmlDocument);//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DTD<54>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (html != string.Empty)
|
|||
|
|
{
|
|||
|
|
LoadXml(html);//<2F><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>html
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_XmlDocument.Load(fileName);//<2F><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>xml
|
|||
|
|
}
|
|||
|
|
if (clearCommentNode)
|
|||
|
|
{
|
|||
|
|
RemoveCommentNode();
|
|||
|
|
}
|
|||
|
|
XmlCacheKey = GenerateKey(fileName);
|
|||
|
|
if (!theCache.Contains(XmlCacheKey))
|
|||
|
|
{
|
|||
|
|
SaveToCache(XmlCacheKey, !IsNoClone);
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.WriteLog(err.Message + "filename : " + fileName);
|
|||
|
|
Error.Throw(err.Message + "filename : " + fileName);
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>浽ȫ<E6B5BD><C8AB>Cache<68><65>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="key"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Key<65><79><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="isClone"><3E>Ƿ<EFBFBD><C7B7><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD><EFBFBD>浵</param>
|
|||
|
|
public void SaveToCache(string key, bool isClone)
|
|||
|
|
{
|
|||
|
|
if (_CacheMinutes > 0)
|
|||
|
|
{
|
|||
|
|
SaveToCache(key, isClone, _CacheMinutes);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>浽ȫ<E6B5BD><C8AB>Cache<68><65>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="key"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Key<65><79><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="isClone"><3E>Ƿ<EFBFBD><C7B7><EFBFBD>¡<EFBFBD><C2A1><EFBFBD><EFBFBD><EFBFBD>浵</param>
|
|||
|
|
/// <param name="cacheTimeMinutes"><3E>浵<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
public void SaveToCache(string key, bool isClone, double cacheTimeMinutes)
|
|||
|
|
{
|
|||
|
|
if (_XmlDocument != null)
|
|||
|
|
{
|
|||
|
|
if (!isClone)
|
|||
|
|
{
|
|||
|
|
theCache.Set(key, _XmlDocument, cacheTimeMinutes, _FileName);//<2F><><EFBFBD><EFBFBD>Cache<68><65><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
theCache.Set(key, GetCloneFrom(_XmlDocument), cacheTimeMinutes, _FileName);//<2F><><EFBFBD><EFBFBD>Cache<68><65><EFBFBD><EFBFBD>Clone
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool Save()
|
|||
|
|
{
|
|||
|
|
return Save(_FileName);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <param name="fileName">ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7></param>
|
|||
|
|
public bool Save(string fileName)
|
|||
|
|
{
|
|||
|
|
if (Path.GetFileName(fileName).IndexOfAny(AppConst.InvalidFileNameChars) > -1)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>·<EFBFBD><C2B7><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt("XHtmlBase.Save : InvalidPath : " + fileName);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
string xHtml = string.Empty;
|
|||
|
|
if (_XmlDocument != null && _XmlDocument.InnerXml.Length > 0)
|
|||
|
|
{
|
|||
|
|
xHtml = _XmlDocument.InnerXml.Replace(AppConfig.XHtml.DtdUri, "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
if (!string.IsNullOrEmpty(xHtml))
|
|||
|
|
{
|
|||
|
|
if (Directory.Exists(Path.GetDirectoryName(fileName)))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
File.WriteAllText(fileName, xHtml, _Encoding);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
Thread.Sleep(20);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
File.WriteAllText(fileName, xHtml, _Encoding);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt(err);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt("No exist path folder:" + fileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private XmlDocument GetCloneFrom(XmlDocument xDoc)
|
|||
|
|
{
|
|||
|
|
XmlDocument newDoc = new XmlDocument();
|
|||
|
|
//if (xnm != null)// && !AppConfig.XHtml.UseFileLoadXml
|
|||
|
|
//{
|
|||
|
|
// ResolverDtd.Resolver(ref newDoc); //<2F><><EFBFBD><EFBFBD>Ҫָ<D2AA><D6B8><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DTD<54>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>ѱ<EFBFBD><D1B1>滻ָ<E6BBBB>ء<F2B1BEB5>
|
|||
|
|
//}
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
newDoc.LoadXml(xDoc.InnerXml);
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt(err);
|
|||
|
|
newDoc.InnerXml = xDoc.InnerXml;
|
|||
|
|
}
|
|||
|
|
return newDoc;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected XmlNode Fill(string xPath, XmlNode parent)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (parent != null)
|
|||
|
|
{
|
|||
|
|
return parent.SelectSingleNode(xPath.Replace("//", "descendant::"), xnm);
|
|||
|
|
}
|
|||
|
|
return _XmlDocument.SelectSingleNode(xPath, xnm);
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected XmlNodeList Select(string xPath, XmlNode parent)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (parent != null)
|
|||
|
|
{
|
|||
|
|
return parent.SelectNodes(xPath.Replace("//", "descendant::"), xnm);
|
|||
|
|
}
|
|||
|
|
return _XmlDocument.SelectNodes(xPath, xnm);
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected XmlElement Create(string tag)
|
|||
|
|
{
|
|||
|
|
if (xnm == null)
|
|||
|
|
{
|
|||
|
|
return _XmlDocument.CreateElement(tag);
|
|||
|
|
}
|
|||
|
|
return _XmlDocument.CreateElement(tag, xnm.LookupNamespace(PreXml));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected string GetXPath(string tag, string attr, string value)
|
|||
|
|
{
|
|||
|
|
string xPath = "//" + (xnm != null ? PreXml + ":" : "") + tag; //+ "[@" + attr + "='" + value + "']";
|
|||
|
|
if (!string.IsNullOrEmpty(attr))
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(value))
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD>Դ<EFBFBD>Сд<D0A1><D0B4><EFBFBD><EFBFBD>
|
|||
|
|
//xPath += "[@" + attr + "='" + value + "']";
|
|||
|
|
xPath += "[translate(@" + attr + ",'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" + value.ToLower() + "']";
|
|||
|
|
//xPath += "[@" + attr + "=translate('" + value + "','ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')]";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
xPath += "[@" + attr + "]";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return xPath;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>CDATA
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="text"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public string SetCDATA(string text)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(text))
|
|||
|
|
{
|
|||
|
|
return text;
|
|||
|
|
}
|
|||
|
|
text = text.Replace(AppConfig.XHtml.CDataLeft, string.Empty).Replace(AppConfig.XHtml.CDataRight, string.Empty);
|
|||
|
|
text = text.Replace("<![CDATA[", "<![CDATA[").Replace("]]>", "]]>");
|
|||
|
|
//text = text.Replace(((char)10).ToString(), "<BR>");
|
|||
|
|
//text = text.Replace(((char)13).ToString(), "<BR>");
|
|||
|
|
//text = text.Replace(((char)34).ToString(), """);
|
|||
|
|
//text = text.Replace(((char)39).ToString(), "'");
|
|||
|
|
text = text.Replace("\\", "#!!#").Replace("\0", "#!0!#");
|
|||
|
|
text = Filter(text);
|
|||
|
|
return AppConfig.XHtml.CDataLeft + text + AppConfig.XHtml.CDataRight;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>CDATA
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="text"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public string ClearCDATA(string text)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(text))
|
|||
|
|
{
|
|||
|
|
return text;
|
|||
|
|
}
|
|||
|
|
text = text.Replace("#!!#", "\\").Replace("#!0!#", "\\0");
|
|||
|
|
text = text.Replace(AppConfig.XHtml.CDataLeft, string.Empty).Replace(AppConfig.XHtml.CDataRight, string.Empty);
|
|||
|
|
return text;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>滻<EFBFBD><E6BBBB>MMSǰ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="text"><3E><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public string ClearMMS(string text)
|
|||
|
|
{
|
|||
|
|
return text.Replace("MMS::", string.Empty).Replace("::MMS", string.Empty);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>XML(ʮ<><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ 0x1D)<29><>Ч<EFBFBD><D0A7><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ͬʱ<CDAC>滻><3B><><EFBFBD>ţ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
protected string Filter(string text)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(text))
|
|||
|
|
{
|
|||
|
|
return text;
|
|||
|
|
}
|
|||
|
|
StringBuilder info = new StringBuilder(text.Length + 20);
|
|||
|
|
for (int i = 0; i < text.Length; i++)
|
|||
|
|
{
|
|||
|
|
char c = text[i];
|
|||
|
|
if (((c >= 0) && (c <= 8)) || ((c >= 11) && (c <= 12)) || ((c >= 14) && (c <= 32)))
|
|||
|
|
{
|
|||
|
|
info.AppendFormat(" ", c);//&#x{0:X};
|
|||
|
|
}
|
|||
|
|
else if (c == '&')
|
|||
|
|
{
|
|||
|
|
info.Append("&");
|
|||
|
|
// &(<28><EFBFBD><DFBC><EFBFBD>) &
|
|||
|
|
//<(С<><D0A1>) <
|
|||
|
|
//>(<28><><EFBFBD><EFBFBD>) >
|
|||
|
|
//"(˫<><CBAB><EFBFBD><EFBFBD>) "
|
|||
|
|
//'(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) '
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (i > 50 && i != text.Length - 1)
|
|||
|
|
{
|
|||
|
|
char nc = text[i + 1];
|
|||
|
|
if (c == '<' && nc != '/' && nc != '!' && !IsEnChar(nc)) // <20><>Ӣ<EFBFBD><D3A2><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8>
|
|||
|
|
{
|
|||
|
|
info.Append("<");
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
info.Append(c);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return info.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool IsEnChar(char c)//Ӣ<><D3A2><EFBFBD><EFBFBD>ĸ
|
|||
|
|
{
|
|||
|
|
return (c > 64 && c < 91) || (c > 96 && c < 123);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
///// <summary>
|
|||
|
|
///// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻<EFBFBD><E6BBBB>
|
|||
|
|
///// </summary>
|
|||
|
|
//private string RegexReplace(string text)
|
|||
|
|
//{
|
|||
|
|
// Regex regex = new Regex(@"</?[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|||
|
|
// MatchCollection collection = regex.Matches(text);
|
|||
|
|
// foreach (Match mat in collection)
|
|||
|
|
// {
|
|||
|
|
// string value = mat.Value;
|
|||
|
|
// }
|
|||
|
|
// return text;
|
|||
|
|
//}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IDisposable <EFBFBD><EFBFBD>Ա
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ͷ<EFBFBD><CDB7><EFBFBD>Դ
|
|||
|
|
/// </summary>
|
|||
|
|
public virtual void Dispose()
|
|||
|
|
{
|
|||
|
|
if (_XmlDocument != null)
|
|||
|
|
{
|
|||
|
|
//if (!ReadOnly)
|
|||
|
|
//{
|
|||
|
|
//xmlDoc.RemoveAll();
|
|||
|
|
//}
|
|||
|
|
_XmlDocument = null;
|
|||
|
|
//GC.Collect();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|