XML操作总结
来源:互联网 发布:黑界扣字滚刀软件 编辑:程序博客网 时间:2024/05/16 14:09
需要读取的XML文件的格式:
<?xml version="1.0" encoding="utf-8" ?>
<XmlConfig>
<!--连接登入视频服务器字符串。连接服务器视频IP地址;登入视频服务器用户名和密码-->
<ConnectionString ServerIp="127.0.0.1" LoginName="gis" LoginPassword="gis"></ConnectionString>
<!--大屏参数。单个屏幕的大小(1024*768),行列数量(8行6列)-->
<ScreenSize Width="1024" Height="768" RowCount="8" ColumnCount="6"></ScreenSize>
<!--重要视频列表(如:市委、市民中心、市公安局等)。AutoPlay表示在加载视频后是否自动进行播放。-->
<ImportantVideo AutoPlay="false">
<!--视频编号列表-->
<!--市民中心顶(南侧)-->
<VideoNo Value="12345648789" />
<!--市委党校西南门口前绿化带-->
<VideoNo Value="31234645645" />
<!--深南大道南侧招商银行广场前-->
<VideoNo Value="98789789712" />
<!--火车站东广场中巴站出口-->
<VideoNo Value="98798451123" />
</ImportantVideo>
</XmlConfig>
读取XML文件的具体类和接口:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Iaspec.LargeScreenPGIS.Common
{
/// <summary>
/// 对象xml系列化反系列化器
/// </summary>
public interface IXmlSerialize
{
/// <summary>
/// 从一个Xml节点获取其属性对象.
/// </summary>
/// <param name="xmlElement"></param>
object FromXmlElement(XmlElement xmlElement);
/// <summary>
/// 将属性写入到一个XML节点中
/// </summary>
/// <param name="xmlDocument">所要序列化的文档</param>
/// <param name="thisElement"></param>
/// <returns></returns>
XmlElement ToXmlElement(XmlDocument xmlDocument, XmlElement thisElement);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Iaspec.LargeScreenPGIS.Common;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using log4net;
namespace Iaspec.LargeScreenPGIS.Video
{
/// <summary>
/// 视频控件需要的参数配置
/// </summary>
public class VideoConfig : IXmlSerialize
{
private ILog _log;
private string _xmlPath;
private XmlDocument _doc;
private const string _connectionStringKey = "ConnectionString";
private const string _screenSizeKey = "ScreenSize";
private const string _importantVideoKey = "ImportantVideo";
public VideoConfig()
: this(Application.StartupPath + @"/VideoConfig.xml")
{ }
public VideoConfig(string xmlPath)
{
this._log = LogManager.GetLogger("视频配置文件");
this._xmlPath = xmlPath;
//this.LoadXML(this._xmlPath);
}
/// <summary>
/// 加载配置文件
/// </summary>
public void LoadXML()
{
this._xmlPath = Application.StartupPath + @"/VideoConfig.xml";
this.LoadXML(this._xmlPath);
}
/// <summary>
/// 加载配置文件
/// </summary>
/// <param name="xmlPath"></param>
public void LoadXML(string xmlPath)
{
this._doc = new XmlDocument();
try
{
if (!File.Exists(xmlPath))
{
return;
}
this._doc.Load(xmlPath);
this.FromXmlElement(this._doc.DocumentElement);
}
catch (Exception objEx)
{
this._log.Error("加载视频配置文件出错,异常信息:" + objEx.Message);
}
}
public void Save()
{
this.Save(this._xmlPath);
}
/// <summary>
/// 保存配置文件
/// </summary>
public void Save(string filePath)
{
this.ToXmlElement(this._doc, this._doc.DocumentElement);
try
{
this._doc.Save(filePath);
}
catch (Exception objEx)
{
this._log.Error("保存视频配置文件出错,异常信息:" + objEx.Message);
}
}
private VideoConnection _connection = new VideoConnection();
public VideoConnection Connection
{
get { return _connection; }
set { _connection = value; }
}
private ScreenSize _screenSize = new ScreenSize();
public ScreenSize ScreenSize
{
get { return _screenSize; }
set { _screenSize = value; }
}
private ImportantVideo _importantVideo = new ImportantVideo();
public ImportantVideo ImportantVideo
{
get { return _importantVideo; }
set { _importantVideo = value; }
}
#region IXmlSerialize 成员
public object FromXmlElement(XmlElement xmlElement)
{
if (xmlElement != null)
{
this._connection = new VideoConnection();
this._connection.FromXmlElement((xmlElement.SelectSingleNode(_connectionStringKey)) as XmlElement);
this._screenSize = new ScreenSize();
this._screenSize.FromXmlElement((xmlElement.SelectSingleNode(_screenSizeKey)) as XmlElement);
this._importantVideo = new ImportantVideo();
this._importantVideo.FromXmlElement((xmlElement.SelectSingleNode(_importantVideoKey)) as XmlElement);
}
return this;
}
public System.Xml.XmlElement ToXmlElement(XmlDocument xmlDocument, XmlElement thisElement)
{
if (this._connection != null)
{
this._connection.ToXmlElement(xmlDocument, (thisElement.SelectSingleNode(_connectionStringKey)) as XmlElement);
}
if (this._screenSize != null)
{
this._screenSize.ToXmlElement(xmlDocument, (thisElement.SelectSingleNode(_screenSizeKey)) as XmlElement);
}
if (this._importantVideo != null)
{
this._importantVideo.ToXmlElement(xmlDocument, (thisElement.SelectSingleNode(_importantVideoKey)) as XmlElement);
}
return thisElement;
}
#endregion
}
/// <summary>
/// 连接登入视频服务器配置
/// </summary>
public class VideoConnection : IXmlSerialize
{
private const string _serverIpKey = "ServerIp";
private const string _loginNameKey = "LoginName";
private const string _loginPasswordKey = "LoginPassword";
public VideoConnection()
{ }
public VideoConnection(string serverIp,string loginName,string loginPassword)
{
this._serverIp = serverIp;
this._loginName = loginName;
this._loginPassword = loginPassword;
}
private string _serverIp = "";
/// <summary>
/// 连接视频服务的IP地址
/// </summary>
public string ServerIp
{
get { return _serverIp; }
set { _serverIp = value; }
}
private string _loginName = "";
/// <summary>
/// 登入视频服务器的用户名
/// </summary>
public string LoginName
{
get { return _loginName; }
set { _loginName = value; }
}
private string _loginPassword = "";
/// <summary>
/// 登入用户服务器的密码
/// </summary>
public string LoginPassword
{
get { return _loginPassword; }
set { _loginPassword = value; }
}
#region IXmlSerialize 成员
public object FromXmlElement(XmlElement xmlElement)
{
string serverIp = xmlElement.GetAttribute(_serverIpKey).Trim();
string loginName = xmlElement.GetAttribute(_loginNameKey).Trim();
string loginPassword = xmlElement.GetAttribute(_loginPasswordKey).Trim();
if (!string.IsNullOrEmpty(serverIp))
{
this._serverIp = serverIp;
}
if (!string.IsNullOrEmpty(loginName))
{
this._loginName = loginName;
}
if (!string.IsNullOrEmpty(loginPassword))
{
this._loginPassword = loginPassword;
}
return this;
}
public XmlElement ToXmlElement(XmlDocument xmlDocument, XmlElement thisElement)
{
thisElement.SetAttribute(_serverIpKey, _serverIp);
thisElement.SetAttribute(_loginNameKey, _loginName);
thisElement.SetAttribute(_loginPasswordKey, _loginPassword);
return thisElement;
}
#endregion
}
/// <summary>
/// 屏幕大小
/// </summary>
public class ScreenSize : IXmlSerialize
{
private const string _widthKey = "Width";
private const string _heightKey = "Height";
private const string _rowCountKey = "RowCount";
private const string _columnCountKey = "ColumnCount";
public ScreenSize()
{ }
public ScreenSize(int width, int height, int rowCount, int columnCount)
{
this._width = width;
this._height = height;
this._rowCount = rowCount;
this._columnCount = columnCount;
}
private double _width = 1024;
/// <summary>
/// 宽度-默认1024
/// </summary>
public double Width
{
get { return _width; }
set { _width = value; }
}
private double _height = 768;
/// <summary>
/// 高度-默认768
/// </summary>
public double Height
{
get { return _height; }
set { _height = value; }
}
private int _rowCount = 8;
/// <summary>
/// 屏行数-默认8
/// </summary>
public int RowCount
{
get { return _rowCount; }
set { _rowCount = value; }
}
private int _columnCount = 6;
/// <summary>
/// 屏列数-默认6
/// </summary>
public int ColumnCount
{
get { return _columnCount; }
set { _columnCount = value; }
}
private int _largeWidth = 8192;
/// <summary>
/// 宽度-默认8192
/// </summary>
public int LargeWidth
{
get { return _largeWidth * _rowCount; }
}
private int _largeHeight = 4608;
/// <summary>
/// 高度-默认4608
/// </summary>
public int LargeHeight
{
get { return _largeHeight * _columnCount; }
}
#region IXmlSerialize 成员
public object FromXmlElement(XmlElement xmlElement)
{
string largeWidth =xmlElement.GetAttribute(_widthKey).Trim();
string largeHeight = xmlElement.GetAttribute(_heightKey).Trim();
string rowCount = xmlElement.GetAttribute(_rowCountKey).Trim();
string columnCount = xmlElement.GetAttribute(_columnCountKey).Trim();
try
{
if (!string.IsNullOrEmpty(largeWidth))
{
this._largeWidth = Convert.ToInt32(largeWidth);
}
if (!string.IsNullOrEmpty(largeHeight))
{
this._largeHeight = Convert.ToInt32(largeHeight);
}
if (!string.IsNullOrEmpty(rowCount))
{
this._rowCount = Convert.ToInt32(rowCount);
}
if (!string.IsNullOrEmpty(columnCount))
{
this._columnCount = Convert.ToInt32(columnCount);
}
}
catch { }
return this;
}
public XmlElement ToXmlElement(XmlDocument xmlDocument, XmlElement thisElement)
{
thisElement.SetAttribute(_widthKey, _largeWidth.ToString());
thisElement.SetAttribute(_heightKey, _largeHeight.ToString());
thisElement.SetAttribute(_rowCountKey, _rowCount.ToString());
thisElement.SetAttribute(_columnCountKey, _columnCount.ToString());
return thisElement;
}
#endregion
}
/// <summary>
/// 重要视频点
/// </summary>
public class ImportantVideo : IXmlSerialize
{
private const string _autoPlayKey = "AutoPlay";
private const string _videoNoKey = "VideoNo";
private const string _valueKey = "Value";
public ImportantVideo()
{ }
public ImportantVideo(bool auto)
{ }
private bool _autoPlay = false;
public bool AutoPlay
{
get { return _autoPlay; }
set { _autoPlay = value; }
}
private List<string> _videoNoList = new List<string>();
public List<string> VideoNoList
{
get { return _videoNoList; }
set { _videoNoList = value; }
}
#region IXmlSerialize 成员
public object FromXmlElement(XmlElement xmlElement)
{
try
{
string autoPlay = xmlElement.GetAttribute(_autoPlayKey).Trim();
if (!string.IsNullOrEmpty(autoPlay))
{
this._autoPlay = Convert.ToBoolean(autoPlay);
}
XmlNodeList videoNoLists = xmlElement.ChildNodes;
if (videoNoLists != null && videoNoLists.Count > 0)
{
foreach (XmlNode xmlNode in videoNoLists)
{
if (xmlNode.Name == _videoNoKey)
{
string videoNo = (xmlNode as XmlElement).GetAttribute(_valueKey).Trim();
//string videoNo = xmlNode.InnerText;
if (!this._videoNoList.Contains(videoNo))
this._videoNoList.Add(videoNo);
}
}
}
}
catch
{ }
return this;
}
public XmlElement ToXmlElement(XmlDocument xmlDocument, XmlElement thisElement)
{
thisElement.SetAttribute(_autoPlayKey, Convert.ToString(_autoPlay));
XmlNodeList videoNoLists = thisElement.SelectSingleNode(_videoNoKey).ChildNodes;
for (int i = 0; i < this._videoNoList.Count; i++)
{
if (videoNoLists[i] != null && videoNoLists[i].InnerText != this._videoNoList[i])
videoNoLists[i].InnerText = this._videoNoList[i];
}
return thisElement;
}
#endregion
}
}
- xml操作总结
- XML操作总结
- XML操作总结
- XML操作总结
- Java XML操作总结
- Flex 操作XML总结
- XML操作总结
- C#操作XML总结
- CMarkup类操作Xml总结
- Dom4J_XPath的XML操作总结
- xml文件操作的总结
- Dom4J_XPath的XML操作总结
- CMarkup类操作Xml总结
- dom4j解析xml操作总结
- CMarkup类操作Xml总结
- vb 操作xml方法总结
- CMarkup类操作Xml总结
- PHPl使用DOM XML操作XML[总结]
- SQL在存储过程编写经验和优化措施
- CSMA/CA
- R6025
- UKF备忘录
- Python调用C/C++的种种方法
- XML操作总结
- Android源码学习之七—传感器的背后
- 2010年崭新的旅程
- 矩阵乘方和(POJ 3233)
- linux/compile.h头文件说明
- android定制个性化的View
- iphone 学习点滴
- Bayeux协议
- qt得到本机ip和创建socket