コード例 #1
0
        /// <summary>
        /// 将模板转换为XML
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string ToXml(DimensionalTemplate model)
        {
            //TODO:转换成xml结构的算法
            //创建一个xml对象
            XmlDocument xmlDoc = new XmlDocument();
            //创建开头
            XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

            xmlDoc.AppendChild(dec);
            //创建根节点
            XmlElement root = xmlDoc.CreateElement("Root");

            root.SetAttribute("DPI", model.DPI.ToString());
            root.SetAttribute("Height", model.Height.ToString());
            root.SetAttribute("Width", model.Width.ToString());
            root.SetAttribute("Name", model.Name);
            root.SetAttribute("PrintHeight", model.PrintHeight.ToString());
            root.SetAttribute("PrintWidth", model.PrintWidth.ToString());
            root.SetAttribute("SeatCodeCount", model.SeatCodeCount.ToString());

            //包含的元素
            XmlElement ElementNode = xmlDoc.CreateElement("ElementList");

            foreach (DimensionalElement element in model.ElementList)
            {
                XmlElement ThrNode = xmlDoc.CreateElement("Element");
                ThrNode.SetAttribute("Alignment", element.Alignment.ToString());
                ThrNode.SetAttribute("Angle", element.Angle.ToString());
                ThrNode.SetAttribute("FontColor", element.FontColor);
                ThrNode.SetAttribute("FontSize", element.FontSize.ToString());
                ThrNode.SetAttribute("Height", element.Height.ToString());
                ThrNode.SetAttribute("Width", element.Width.ToString());
                ThrNode.SetAttribute("ImageFile", element.ImageFile);
                ThrNode.SetAttribute("Order", element.Order.ToString());
                ThrNode.SetAttribute("PosintionX", element.PosintionX.ToString());
                ThrNode.SetAttribute("PosintionY", element.PosintionY.ToString());
                ThrNode.SetAttribute("Text", element.Text);
                ThrNode.SetAttribute("Type", element.Type.ToString());
                ElementNode.AppendChild(ThrNode);
            }
            root.AppendChild(ElementNode);

            //模板包含的文件
            XmlElement FileNode = xmlDoc.CreateElement("ImageList");

            foreach (string file in model.ImageFiles)
            {
                XmlElement ThrNode = xmlDoc.CreateElement("Image");
                ThrNode.SetAttribute("Path", file);
                FileNode.AppendChild(ThrNode);
            }
            root.AppendChild(FileNode);

            //添加根节点
            xmlDoc.AppendChild(root);
            return(xmlDoc.OuterXml);
        }
コード例 #2
0
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static DimensionalTemplate Parse(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return(null);
            }
            XmlDocument         xmlDoc = new XmlDocument();
            DimensionalTemplate model  = new DimensionalTemplate();

            try
            {
                xmlDoc.LoadXml(xml);
                //查找根节点
                XmlNode node = xmlDoc.SelectSingleNode("//Root");
                model.DPI           = int.Parse(node.Attributes["DPI"].Value);
                model.Height        = double.Parse(node.Attributes["Height"].Value);
                model.Width         = double.Parse(node.Attributes["Width"].Value);
                model.Name          = node.Attributes["Name"].Value;
                model.PrintHeight   = double.Parse(node.Attributes["PrintHeight"].Value);
                model.PrintWidth    = double.Parse(node.Attributes["PrintWidth"].Value);
                model.SeatCodeCount = int.Parse(node.Attributes["SeatCodeCount"].Value);
                //转换元素
                XmlNodeList nodes = xmlDoc.SelectNodes("//Root/ElementList/Element");
                foreach (XmlNode ItemNode in nodes)
                {
                    DimensionalElement element = new DimensionalElement();
                    element.Alignment  = (ElementTextAlignment)System.Enum.Parse(typeof(ElementTextAlignment), ItemNode.Attributes["Alignment"].Value);
                    element.Angle      = double.Parse(ItemNode.Attributes["Angle"].Value);
                    element.FontColor  = ItemNode.Attributes["FontColor"].Value;
                    element.FontSize   = int.Parse(ItemNode.Attributes["FontSize"].Value);
                    element.Height     = double.Parse(ItemNode.Attributes["Height"].Value);
                    element.Width      = double.Parse(ItemNode.Attributes["Width"].Value);
                    element.PosintionX = double.Parse(ItemNode.Attributes["PosintionX"].Value);
                    element.PosintionY = double.Parse(ItemNode.Attributes["PosintionY"].Value);
                    element.ImageFile  = ItemNode.Attributes["ImageFile"].Value;
                    element.Order      = int.Parse(ItemNode.Attributes["Order"].Value);
                    element.Text       = ItemNode.Attributes["Text"].Value;
                    element.Type       = (DimensionalElementTye)System.Enum.Parse(typeof(DimensionalElementTye), ItemNode.Attributes["Type"].Value);
                    model.ElementList.Add(element);
                }
                //转换图片地址
                nodes = xmlDoc.SelectNodes("//Root/ImageList/Image");
                foreach (XmlNode ItemNode in nodes)
                {
                    model.ImageFiles.Add(ItemNode.Attributes["Path"].Value);
                }

                return(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }