自定义信息传递

现在有一系列信息需要传递,处理成自定义的格式,然后接收后再进行解析:

信息自定义格式如下:

@"~~/ActionType^^/Update~~/IncNo^^/S2008053080034~~/BranchKey^^/4~~/IncLevel^^/3A~~/IncType^^/Gambling~~/StationKey^^/100~~/Status^^/CLOSED~~/AlarmTime^^/2008-05-30 13:02:39~~/Alarmer^^/sadasdasd~~/ContactNo^^/13066891084~~/CreationTime^^/2008-05-01 13:03:45~~/AlarmTelephone^^/13066891084~~/DespatcherUnitKey^^/100~~/IsRequireReinforcement^^/0~~/Creator^^/18064~~/AlarmID^^/200805301302360144009~~/TracedUnitKeyID^^/100~~/OldBranchKey^^/4~~/OldStationKey^^/100~~/OldTracedUnitKeyID^^/100~~/AddrID^^/U247~~/AddrType^^/未知~~/X^^/0~~/Y^^/0~~/Address^^/坳贝二村商业街坳新村19-15";

 

最终要转化为如下格式:

namespace Iaspec.JWPGIS.Model
{
    [Serializable]
    public class CaseInfo
    {
        public CaseInfo();

        public string Address { get; set; }
        public string CaseDesc { get; set; }
        public string CaseNo { get; set; }
        public string CaseStatus { get; set; }
        public string CaseType { get; set; }
        public DateTime CreateTime { get; set; }
        public string Reportor { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
    }
}

 

解析过程如下:

namespace TestCase
{
    class Program
    {
        #region 转换参数

        public const string ActionType = @"~~/ActionType^^/Update~~/";
        public const string ItemSplit = @"~~/";
        public const string FieldSplit = @"^^/";
        private const string CaseType = "IncType^^//";
        private const string Address = "Address^^//";
        private const string X = "X^^//";
        private const string Y = "Y^^//";
        private const string CreateTime = "AlarmTime^^//";
        private const string Reportor = "Alarmer^^//";
        private const string CaseDesc = "remark^^//";

        #endregion

 

        static void Main(string[] args)
        {
            string msg = @"~~/ActionType^^/Update~~/IncNo^^/S2008053080034~~/BranchKey^^/4~~/IncLevel^^/3A~~/IncType^^/Gambling~~/StationKey^^/100~~/Status^^/CLOSED~~/AlarmTime^^/2008-05-30 13:02:39~~/Alarmer^^/sadasdasd~~/ContactNo^^/13066891084~~/CreationTime^^/2008-05-01 13:03:45~~/AlarmTelephone^^/13066891084~~/DespatcherUnitKey^^/100~~/IsRequireReinforcement^^/0~~/Creator^^/18064~~/AlarmID^^/200805301302360144009~~/TracedUnitKeyID^^/100~~/OldBranchKey^^/4~~/OldStationKey^^/100~~/OldTracedUnitKeyID^^/100~~/AddrID^^/U247~~/AddrType^^/未知~~/X^^/0~~/Y^^/0~~/Address^^/坳贝二村商业街坳新村19-15";


            List<string> results = new List<string>();

            int index = 0;
            int len = 0;

            for (int i = 0; i < msg.Length; i++)
            {
                if (i + ItemSplit.Length > msg.Length)
                    break;
                string subStr = msg.Substring(i, ItemSplit.Length);
                if (subStr == ItemSplit)
                {
                    len = i - index;
                    results.Add(msg.Substring(index, len));
                    i = index = i + ItemSplit.Length;
                }
            }
            results.Add(msg.Substring(index, msg.Length - index));

            ConvertToAlarmInfo(results);
        }

        private static Iaspec.JWPGIS.Model.CaseInfo ConvertToAlarmInfo(List<string> results)
        {
            if (results == null || results.Count == 0)
                return null;

            Iaspec.JWPGIS.Model.CaseInfo info = new Iaspec.JWPGIS.Model.CaseInfo();
            foreach (string str in results)
            {
                if (str.IndexOf(CaseType) == 0)
                    info.CaseType = str.Substring(CaseType.Length, str.Length - CaseType.Length);
                if (str.IndexOf(Address) == 0)
                    info.Address = str.Substring(Address.Length, str.Length - Address.Length);
                if (str.IndexOf(X) == 0 && !string.IsNullOrEmpty(str.Substring(X.Length, str.Length - X.Length)))
                    info.X = double.Parse(str.Substring(X.Length, str.Length - X.Length));
                if (str.IndexOf(Y) == 0 && !string.IsNullOrEmpty(str.Substring(Y.Length, str.Length - Y.Length)))
                    info.Y = double.Parse(str.Substring(Y.Length, str.Length - Y.Length));
                if (str.IndexOf(CreateTime) == 0)
                    info.CreateTime = DateTime.Parse(str.Substring(CreateTime.Length, str.Length - CreateTime.Length));
                if (str.IndexOf(Reportor) == 0)
                    info.Reportor = str.Substring(Reportor.Length, str.Length - Reportor.Length);
                if (str.IndexOf(CaseDesc) == 0)
                    info.CaseDesc = str.Substring(CaseDesc.Length, str.Length - CaseDesc.Length);
            }
            return info;
        }
    }
}

总结:其实这里的思路就像是网络协议一样,自定义了“报头”,然后读到信息的时候再解析出来,这里的报头就是 @"~~/",这里属性与值之间的间隔就是@"^^/",通过这种自定义的符号,就可以把各个属性及其对应的值解析出来存到list中,上面红色代码完成这个工作。

然后在下面这个方法中,其实就是将解析的信息填充到类中去。

上面的红色代码,自认为写的还好。特此总结。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值