public void LoadFromFileName(string fileName) { Stream stream = new FileStream(fileName, FileMode.Open); BinaryReader binaryReader = new BinaryReader(stream, System.Text.Encoding.Unicode); byte[] buffer = binaryReader.ReadBytes((int)stream.Length); //string xmlContent = File.ReadAllText(fileName); try { string xmlContent = System.Text.Encoding.Unicode.GetString(buffer); binaryReader.Close(); stream.Close(); if (xmlContent.IndexOf("<SnControl.Solution>") == -1) { xmlContent = File.ReadAllText(fileName); } string xmlSolutionContent = xmlContent.Substring(xmlContent.IndexOf("<SnControl.Solution>"), xmlContent.IndexOf("</SnControl.Solution>") - xmlContent.IndexOf("<SnControl.Solution>")); xmlSolutionContent += "</SnControl.Solution>"; string xmlFormContent = xmlContent.Substring(xmlContent.IndexOf("<System.Windows.Forms.Form"), xmlContent.IndexOf("</QuerySolution>") - xmlContent.IndexOf("<System.Windows.Forms.Form")); XmlSolutionReader xmlSolutionReader = new XmlSolutionReader(_solution); _solution = xmlSolutionReader.SetUpSolution(xmlSolutionContent); if (_solution == null) { Controls.Clear(); return; } SolutionInstance.GetInstance().Solution = _solution; IDesignerLoaderProvider loaderProvider = new XmlDesignerLoaderProvider(xmlFormContent); IDesignerGenerator generator = new XmlDesignerGenerator(); _designerViewContent = new FormsDesignerViewContent(this, loaderProvider, generator); //加载XML内容 _designerViewContent.Reload(); _designPanel = new DesignPanel(_designerViewContent.DesignSurface); _designPanel.Dock = DockStyle.Fill; Controls.Add(_designPanel); if (!_designPanel.SetRootDesigner()) { Controls.Clear(); return; } _designerViewContent.PropertyContainer.Host = _designPanel.Host; _designerViewContent.PropertyContainer.SelectableObjects = _designerViewContent.Host.Container.Components; } catch (ApplicationException e) { MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void FormQueryResult_Load(object sender, EventArgs e) { if (DesignMode) { return; } Stream stream = new FileStream(FileName, FileMode.Open); BinaryReader binaryReader = new BinaryReader(stream, System.Text.Encoding.Unicode); byte[] buffer = binaryReader.ReadBytes((int)stream.Length); //string xmlContent = File.ReadAllText(fileName); string xmlContent; string solutionContent; string xmlForm = string.Empty; XmlDocument doc = new XmlDocument(); try { xmlContent = System.Text.Encoding.Unicode.GetString(buffer); binaryReader.Close(); stream.Close(); if (xmlContent.IndexOf("<SnControl.Solution>") == -1) { xmlContent = File.ReadAllText(FileName); } solutionContent = xmlContent.Substring(xmlContent.IndexOf("<SnControl.Solution>"), xmlContent.IndexOf("</SnControl.Solution>") - xmlContent.IndexOf("<SnControl.Solution>")) + "</SnControl.Solution>"; XmlSolutionReader xmlSolutionReader = new XmlSolutionReader(_solution); _solution = xmlSolutionReader.SetUpSolution(solutionContent); SolutionInstance.GetInstance().Solution = _solution; Text = _solution.SolutionName; xmlForm = xmlContent.Substring(xmlContent.IndexOf("<System.Windows.Forms.Form"), xmlContent.IndexOf("</QuerySolution>") - xmlContent.IndexOf("<System.Windows.Forms.Form")); doc.LoadXml(xmlForm); } catch (ApplicationException ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (doc.DocumentElement.Attributes["version"] == null) { CreateObject(xmlForm); } else { foreach (XmlElement element in doc.DocumentElement.ChildNodes) { CreateObject(element, true); } } string resultType = ""; object result = string.Empty; IEnumerator enumControl = _componentContainer.GetEnumerator(); Method method = new Method(this._dao); while (enumControl.MoveNext()) { var ctl = enumControl.Current as ICommonAttribute; if (ctl == null) { continue; } if (string.IsNullOrEmpty(ctl.Function)) { continue; } method.ExecMethod(ctl.Function, out resultType, out result); switch (resultType) { case "字符": case "日期": ((Control)ctl).Text = result.ToString(); break; case "数据集": if (ctl is ParamComboBox) { ((ParamComboBox)ctl).DataSource = ((DataTable)result).DefaultView; ((ParamComboBox)ctl).SelectedItem = null; } break; default: break; } } Size = new Size(Size.Width, Size.Height + 1); Size = new Size(Size.Width, Size.Height - 1); }