⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsstagbase.cs

📁 该项目中对 SQLHelper 类进行了简单封装
💻 CS
字号:
/* 
 * RssTagBase.cs @Microsoft Visual Studio 2008 <.NET Framework 3.5>
 * AfritXia
 * 2008-01-31
 * 
 * Copyright(c) http://www.AfritXia.NET/
 * 
 */

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace NET.AfritXia.RssFoundation
{
	/// <summary>
	/// RSS 标记基础类
	/// </summary>
	public abstract class RssTagBase : IXmlSerializable
	{
		// 属性字典
		private IDictionary<string, object> m_attrHash = null;
		// 子标记字典
		private IDictionary<Type, RssTagBase> m_childTagHash = null;
		// 标记值
		private object m_value = null;

		#region 类构造器
		/// <summary>
		/// 类默认构造器
		/// </summary>
		public RssTagBase()
		{
		}
		#endregion

		/// <summary>
		/// 获取标记名称
		/// </summary>
		protected abstract string TagName
		{
			get;
		}

		/// <summary>
		/// 设置或获取标记值
		/// </summary>
		public virtual object Value
		{
			set
			{
				this.m_value = value;
			}

			get
			{
				return m_value;
			}
		}

		/// <summary>
		/// 获取属性字典
		/// </summary>
		protected IDictionary<string, object> AttributeHash
		{
			get
			{
				return this.m_attrHash;
			}
		}

		/// <summary>
		/// 获取子标记字典
		/// </summary>
		protected IDictionary<Type, RssTagBase> ChildTagHash
		{
			get
			{
				return this.m_childTagHash;
			}
		}

		/// <summary>
		/// 添加属性值
		/// </summary>
		/// <param name="attrName">属性名称</param>
		/// <param name="attrValue">属性值</param>
		protected void AddAttribute(string attrName, object attrValue)
		{
			if (this.m_attrHash == null)
				this.m_attrHash = new Dictionary<string, object>();

			this.m_attrHash[attrName] = attrValue;
		}

		/// <summary>
		/// 获取属性值
		/// </summary>
		/// <param name="attrName">属性名称</param>
		/// <returns></returns>
		protected object GetAttribute(string attrName)
		{
			if (this.m_attrHash == null)
				return null;

			return this.m_attrHash[attrName];
		}

		/// <summary>
		/// 移除属性
		/// </summary>
		/// <param name="attrName">被移除的属性名称</param>
		protected void RemoveAttribute(string attrName)
		{
			if (this.m_attrHash == null)
				return;

			this.m_attrHash.Remove(attrName);
		}

		/// <summary>
		/// 清除所有属性
		/// </summary>
		protected void ClearAttributes()
		{
			if (this.m_attrHash == null)
				return;

			this.m_attrHash.Clear();
		}

		/// <summary>
		/// 添加子标记
		/// </summary>
		/// <param name="childTag"></param>
		protected void AddChildTag(RssTagBase childTag)
		{
			if (this.m_childTagHash == null)
				this.m_childTagHash = new Dictionary<Type, RssTagBase>();

			this.m_childTagHash[childTag.GetType()] = childTag;
		}

		/// <summary>
		/// 获取子标记
		/// </summary>
		/// <typeparam name="TTag">子标记类型</typeparam>
		/// <returns></returns>
		protected TTag GetChildTag<TTag>() where TTag : RssTagBase
		{
			if (this.m_childTagHash == null)
				return null;

			return this.m_childTagHash[typeof(TTag)] as TTag;
		}

		/// <summary>
		/// 移除子标记
		/// </summary>
		/// <typeparam name="TTag">被移除的子标记类型</typeparam>
		protected void RemoveChildTag<TTag>() where TTag : RssTagBase
		{
			if (this.m_childTagHash == null)
				return;

			this.m_childTagHash.Remove(typeof(TTag));
		}

		/// <summary>
		/// 清除所有子标记
		/// </summary>
		protected void ClearChildTags()
		{
			if (this.m_childTagHash == null)
				return;

			this.m_childTagHash.Clear();
		}

		/// <summary>
		/// 是否为空标记
		/// </summary>
		protected bool IsEmptyTag
		{
			get
			{
				bool value = true;

				// 没有标记值?
				value &= (this.Value == null);
				// 没有定义任何属性?
				value &= (this.m_attrHash == null || this.m_attrHash.Count <= 0);
				// 没有任何子标记?
				value &= (this.m_childTagHash == null || this.m_childTagHash.Count <= 0);

				return value;
			}
		}

		#region IXmlSerializable Members
		public XmlSchema GetSchema()
		{
			return null;
		}

		public void ReadXml(XmlReader reader)
		{
			throw new NotSupportedException();
		}

		public virtual void WriteXml(XmlWriter writer)
		{
			if (writer == null)
				return;

			if (this.IsEmptyTag)
				return;

			// <TagName>
			writer.WriteStartElement(this.TagName);
			{
				if (this.m_attrHash != null)
				{
					foreach (string key in this.m_attrHash.Keys)
					{
						string attrName = key;
						object attrVal = this.m_attrHash[key];

						if (String.IsNullOrEmpty(key) || attrVal == null)
							continue;

						// AttrName = "Value"
						writer.WriteAttributeString(attrName, Convert.ToString(attrVal));
					}
				}

				if (this.m_childTagHash != null)
				{
					foreach (RssTagBase tag in this.m_childTagHash.Values)
						tag.WriteXml(writer);
				}
			}
			// Value
			if (this.Value != null)
				writer.WriteString(Convert.ToString(this.Value));

			// </TagName>
			writer.WriteEndElement();
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -