示例#1
0
        public static void Address()
        {
            //获取当前根路径
            string      path   = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(@path + "\\xmlSource\\Config\\Protocol\\MSPAddress.xml");
            Console.WriteLine(xmlDoc.OuterXml);//输出XML文档
            PointsTypeConnection cc = XmlSerializeUtil.Deserialize(typeof(PointsTypeConnection), xmlDoc.OuterXml) as PointsTypeConnection;

            Console.WriteLine();
            int i = 1;

            /*   foreach (var item in cc.GetPoints)
             * {
             *     i++;
             *     Console.WriteLine("----------------------" + i + "--------------------------------");
             *     Console.WriteLine("PointsType--NameCH:" + item.FunctionCode);
             *     Console.WriteLine("PointsType--ID:" + item.ID);
             *     Console.WriteLine("PointsType--CommType:" + item.SlaveId);
             *
             *     foreach (var point in item.GetPoint)
             *     {
             *         Console.WriteLine("point--BitIndex:" + point.BitIndex);
             *         Console.WriteLine("point--DataBit:" + point.DataBit);
             *         Console.WriteLine("point--Factor:" + point.Factor);
             *         Console.WriteLine("point--LocalAddress:" + point.LocalAddress);
             *         Console.WriteLine("point--ProtocolAddress:" + point.ProtocolAddress);
             *         Console.WriteLine("point--TextContent:" + point.TextContent);
             *
             *
             *     }
             * }
             */
        }
示例#2
0
        public static void WriteAddress()
        {
            Point point = new Point
            {
                BitIndex        = "bitindex",
                DataBit         = "databit",
                Factor          = "factor",
                LocalAddress    = "localaddress",
                ProtocolAddress = "protocoladdress",
                TextContent     = "text"
            };
            Point point2 = new Point
            {
                BitIndex        = "bitindex2",
                DataBit         = "databit2",
                Factor          = "factor2",
                LocalAddress    = "localaddress2",
                ProtocolAddress = "protocoladdress2",
                TextContent     = "text2"
            };
            Point point3 = new Point
            {
                BitIndex        = "bitindex",
                DataBit         = "databit",
                Factor          = "factor",
                LocalAddress    = "localaddress",
                ProtocolAddress = "protocoladdress",
                TextContent     = "text"
            };
            Point point4 = new Point
            {
                BitIndex        = "bitindex2",
                DataBit         = "databit2",
                Factor          = "factor2",
                LocalAddress    = "localaddress2",
                ProtocolAddress = "protocoladdress2",
                TextContent     = "text2"
            };
            PointsConnection points = new PointsConnection
            {
                ID           = "1",
                FunctionCode = "03",
                SlaveId      = "slave",
            };

            points.ID = "11245";
            Point[] pointArr = new Point[] { point, point2 };
            points.PointArray = pointArr;
            PointsConnection points2 = new PointsConnection
            {
                ID           = "12",
                FunctionCode = "032",
                SlaveId      = "slave2",
            };

            Point[] pointArray = new Point[] { point3, point4 };
            points2.PointArray = pointArray;
            PointsTypeConnection pointsType = new PointsTypeConnection();

            PointsConnection[] pointsConnections = new PointsConnection[] { points, points2 };
            pointsType.Add(points); pointsType.Add(points2);
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", "");
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(PointsTypeConnection));

            //将对象序列化输出到控制台
            xmlSerializer.Serialize(Console.Out, pointsType, namespaces);

            /*
             * string xml = XmlSerializeUtil.Serializer(typeof(Points), points);
             *
             * byte[] byteToStr = System.Text.Encoding.Default.GetBytes(xml);
             * string str64 = Convert.ToBase64String(byteToStr);
             * Console.WriteLine(Environment.NewLine + str64);
             *
             * byte[] accessory = Convert.FromBase64String(str64);
             * string fromStr = System.Text.Encoding.Default.GetString(accessory);
             * Console.WriteLine("Base64转字符串" + fromStr);
             * FileStream fileStream = File.Create("test2.xml");
             * fileStream.Write(byteToStr, 0, byteToStr.Length);
             * fileStream.Flush();
             * fileStream.Close();
             */
        }