public override void Run() { DTTableNode owner = this.Owner as DTTableNode; DTTable domainObject = owner.DomainObject; DbProviderFactory factory = DbProviderFactories.GetFactory(domainObject.DTDatabase.DSType); if (factory != null) { using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = domainObject.DTDatabase.DecryptConnectionString; connection.Open(); DbCommand command = connection.CreateCommand(); command.CommandText = string.Format("select * from {0} where 1<>1", domainObject.Name); DbDataReader reader = command.ExecuteReader(); DataTable schemaTable = null; try { schemaTable = reader.GetSchemaTable(); } finally { reader.Close(); } owner.Expand(); UnitOfWork work = new UnitOfWork(typeof(DTColumn)); int num = 0; foreach (DataRow row in schemaTable.Rows) { DTColumn dtColumn = new DTColumn(); dtColumn.DTTable = domainObject; dtColumn.Name = row["ColumnName"].ToString(); dtColumn.Type = row["DataType"].ToString(); dtColumn.MapXMLElementName = "{0}:" + dtColumn.Name; dtColumn.NamespacePrefix = "{0}"; dtColumn.NamespaceUri = "{0}"; dtColumn.DisplayOrder = num++; owner.AddDTColumnNode(dtColumn); work.RegisterNew(dtColumn); } work.Commit(); } } }
public override void Run() { OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Title = "选择..."; dialog.Filter = "schema 文件 (*.xsd)|*.xsd"; int num = 0; if (dialog.ShowDialog() == DialogResult.OK) { DTTableNode owner = this.Owner as DTTableNode; DTTable domainObject = owner.DomainObject; string targetNamespace = string.Format("{0}{1}/{2}", "http://www.eAppxml.gov.cn/", domainObject.DTDatabase.DTProject.MapXMLElementName.Substring(0, 2).ToLower(), domainObject.DTDatabase.DTProject.MapXMLElementName.ToLower()); XmlSchemaSet set = new XmlSchemaSet(); set.Add(targetNamespace, dialog.FileName); set.Compile(); string str2 = string.Empty; foreach (XmlSchema schema in set.Schemas(targetNamespace)) { foreach (XmlSchemaElement element in schema.Elements.Values) { LoggingService.InfoFormatted("XmlSchemaElement.Name=>{0}", new object[] { element.Name }); if (element.Name == domainObject.MapXMLElementName.Replace("{0}:", string.Empty)) { LoggingService.InfoFormatted("ElementSchemaType is {0},{1}", new object[] { element.ElementSchemaType.Name, element.ElementSchemaType.TypeCode }); XmlSchemaComplexType elementSchemaType = element.ElementSchemaType as XmlSchemaComplexType; XmlSchemaSequence contentTypeParticle = elementSchemaType.ContentTypeParticle as XmlSchemaSequence; UnitOfWork work = new UnitOfWork(typeof(DTColumn)); foreach (XmlSchemaElement element2 in contentTypeParticle.Items) { string name = string.Empty; string description = string.Empty; try { name = ((XmlSchemaAttribute) ((XmlSchemaSimpleContentExtension) ((XmlSchemaSimpleContent) ((XmlSchemaComplexType) element2.SchemaType).ContentModel).Content).Attributes[0]).Name; if (element2.Annotation.Items.Count > 0) { description = ((XmlSchemaDocumentation) element2.Annotation.Items[0]).Markup[0].Value; } else { str2 = str2 + string.Format("列:'{0}'没有备注字段,请注意调整设置", name); description = name; } } catch (Exception exception) { LoggingService.Error(exception); str2 = str2 + exception.Message + "\r\n"; } if (!string.IsNullOrEmpty(name)) { DTColumn column = new DTColumn(domainObject.NamespacePrefix, domainObject.NamespaceUri, domainObject, name, description, num * 10, string.Empty, "{0}:" + element2.Name, typeof(string).FullName); work.RegisterNew(column); owner.AddDTColumnNode(column); num++; } } work.Commit(); } } } if (!string.IsNullOrEmpty(str2)) { LoggingService.WarnFormatted("加载完成{0}列,但有错误:\r\n{1}", new object[] { num, str2 }); MessageBox.Show(string.Format("加载完成{0}列,但有错误:\r\n{1}", num, str2)); } else { MessageBox.Show(string.Format("从SCHEMA中成功添加了'{0}'列", num)); } } }
private static void SetColumnValue(XPathDocument[] documents, string namespacePrefix, string namespaceUri, XPathNavigator node, DataRow row, DTColumn dtcolumn) { XPathNavigator navigator; object obj2; Type type; if (LoggingService.IsDebugEnabled) { LoggingService.DebugFormatted("开始设定'{0}<{1}>'的值:", new object[] { dtcolumn.Name, dtcolumn.Description }); } if (!string.IsNullOrEmpty(namespacePrefix) && !string.IsNullOrEmpty(namespaceUri)) { dtcolumn.NamespacePrefix = string.Format(dtcolumn.NamespacePrefix, namespacePrefix); dtcolumn.NamespaceUri = string.Format(dtcolumn.NamespaceUri, namespaceUri); dtcolumn.MapXMLElementName = string.Format(dtcolumn.MapXMLElementName, namespacePrefix); } XPathExpression expression = node.Compile(dtcolumn.MapXMLElementName); XmlNamespaceManager nsManager = null; if (!string.IsNullOrEmpty(dtcolumn.NamespacePrefix)) { nsManager = new XmlNamespaceManager(node.NameTable); nsManager.AddNamespace(dtcolumn.NamespacePrefix, dtcolumn.NamespaceUri); expression.SetContext(nsManager); } string str = null; switch (expression.ReturnType) { case XPathResultType.Number: case XPathResultType.String: case XPathResultType.Boolean: goto Label_02B7; case XPathResultType.NodeSet: navigator = null; try { if (nsManager != null) { navigator = node.SelectSingleNode(expression.Expression, nsManager); } else { navigator = node.SelectSingleNode(expression); } } catch (XPathException) { } if (navigator != null) { goto Label_028F; } if (dtcolumn.MapXMLElementName.StartsWith("/")) { LoggingService.WarnFormatted("找不到节点:{0},将遍历整个树查找", new object[] { expression.Expression }); foreach (XPathDocument document in documents) { if (document != null) { XPathNavigator navigator2 = document.CreateNavigator(); XPathNavigator navigator3 = null; if (!string.IsNullOrEmpty(namespacePrefix) && !string.IsNullOrEmpty(namespaceUri)) { dtcolumn.NamespacePrefix = string.Format(dtcolumn.NamespacePrefix, namespacePrefix); dtcolumn.NamespaceUri = string.Format(dtcolumn.NamespaceUri, namespaceUri); dtcolumn.MapXMLElementName = string.Format(dtcolumn.MapXMLElementName, namespacePrefix); } if (!string.IsNullOrEmpty(dtcolumn.NamespacePrefix)) { XmlNamespaceManager resolver = new XmlNamespaceManager(navigator2.NameTable); resolver.AddNamespace(dtcolumn.NamespacePrefix, dtcolumn.NamespaceUri); navigator3 = navigator2.SelectSingleNode(dtcolumn.MapXMLElementName, resolver); } if (navigator3 != null) { str = navigator3.Value; LoggingService.DebugFormatted("{0}:{1}:{2}", new object[] { navigator3.LocalName, navigator3.InnerXml, navigator3.Value }); break; } LoggingService.WarnFormatted("仍然找不到节点:{0}!", new object[] { dtcolumn.MapXMLElementName }); return; } } goto Label_028F; } LoggingService.WarnFormatted("找不到节点:{0}!", new object[] { dtcolumn.MapXMLElementName }); return; default: goto Label_02E7; } return; Label_028F: try { if (navigator != null) { str = navigator.Value; } goto Label_0306; } catch (Exception exception) { LoggingService.Error(string.Format("不能获取任何指定XPATH表达式{{0}}的节点", dtcolumn.MapXMLElementName), exception); return; } Label_02B7: obj2 = node.Evaluate(expression); try { str = Convert.ToString(obj2); goto Label_0306; } catch (FormatException) { LoggingService.WarnFormatted("获取的XPATH查询值:'{0}'不能转换为字符串!", new object[] { obj2 }); goto Label_0306; } Label_02E7:; LoggingService.WarnFormatted("XPATH表达式{{0}}的解析有误,或其返回结果尚不被支持", new object[] { dtcolumn.MapXMLElementName }); Label_0306: type = row.Table.Columns[dtcolumn.Name].DataType; if (LoggingService.IsDebugEnabled) { LoggingService.DebugFormatted("将设定'{0}<{1}>({3})({4})'的值为'{2}'", new object[] { dtcolumn.Name, dtcolumn.Description, str, dtcolumn.Type, type.FullName }); } if (type == typeof(string)) { try { row[dtcolumn.Name] = str; return; } catch (FormatException) { LoggingService.WarnFormatted("设定'{0}<1>'的值为'{2}'时出错", new object[] { dtcolumn.Name, dtcolumn.Description, str }); return; } } if (!string.IsNullOrEmpty(str)) { if (type.Equals(typeof(DateTime))) { str = str.Replace('一', '1').Replace('二', '2').Replace('三', '3').Replace('四', '4').Replace('五', '5').Replace('六', '6').Replace('七', '7').Replace('八', '8').Replace('九', '9'); } TypeConverter converter = TypeDescriptor.GetConverter(type); if (converter.IsValid(str)) { try { row[dtcolumn.Name] = converter.ConvertFrom(str); return; } catch (FormatException) { LoggingService.WarnFormatted("设定'{0}<1>'的值为'{2}'时出错", new object[] { dtcolumn.Name, dtcolumn.Description, str }); return; } } try { row[dtcolumn.Name] = str; } catch (Exception exception2) { LoggingService.ErrorFormatted("数据交换发生数据转换错误:表{{0}};列{{1}};值{{2}}\r\n{3}", new object[] { dtcolumn.DTTable.Name, dtcolumn.Name, str, exception2 }); } } }