C#中讀取pdm檔案資訊(PowerDesigner讀取方法)
阿新 • • 發佈:2019-01-16
其實PowerDesigner的pdm檔案是一個xml檔案來的,我們可以當作讀取xml檔案一樣的方式來讀pdm檔案。 其中,我們需要注意的是:如果我們不使用名稱空間管理器的話,系統會提示如下錯誤: 需要名稱空間管理器或 XsltContext。此查詢具有字首、變數或使用者定義的函式。 xmlDoc.Load(pdmFile); xmlnsManager =new XmlNamespaceManager(xmlDoc.NameTable); xmlnsManager.AddNamespace("a", "attribute"); xmlnsManager.AddNamespace("c", "collection"); xmlnsManager.AddNamespace("o", "object"); XmlNode xnTables = xmlDoc.SelectSingleNode("//"+ cTables, xmlnsManager); 其實C#讀取pdm檔案難度並不大,只是一種體力活,為了後人少走點彎路,我下面公佈一下程式碼: ColumnInfo.cs(欄位類) 程式碼 namespace Piggy.Common.PDM{ //欄位資訊publicclass ColumnInfo { public ColumnInfo() { } string columnId; publicstring ColumnId { get { return columnId; } set { columnId } string objectID; publicstring ObjectID { get { return objectID; } set { objectID = value; } } string name; publicstring Name { get { return name; } set { name = value; } } string code; publicstring Code { get { return code; } set { code = value; } } int creationDate; publicint CreationDate { get { return creationDate; } set { creationDate = value; } } string creator; publicstring Creator { get { return creator; } set { creator = value; } } int modificationDate; publicint ModificationDate { get { return modificationDate; } set { modificationDate = value; } } string modifier; publicstring Modifier { get { return modifier; } set { modifier = value; } } string comment; publicstring Comment { get { return comment; } set { comment = value; } } string dataType; publicstring DataType { get { return dataType; } set { dataType = value; } } string length; publicstring Length { get { return length; } set { length = value; } } //是否自增量bool identity; publicbool Identity { get { return identity; } set { identity = value; } } bool mandatory; //禁止為空publicbool Mandatory { get { return mandatory; } set { mandatory = value; } } string extendedAttributesText; //擴充套件屬性publicstring ExtendedAttributesText { get { return extendedAttributesText; } set { extendedAttributesText = value; } } string physicalOptions; publicstring PhysicalOptions { get { return physicalOptions; } set { physicalOptions = value; } } } } PdmKey.cs(主鍵類) 程式碼 using System.Collections.Generic;namespace Piggy.Common.PDM { publicclass PdmKey { public PdmKey() { } string keyId; publicstring KeyId { get { return keyId; } set { keyId = value; } } string objectID; publicstring ObjectID { get { return objectID; } set { objectID = value; } } string name; publicstring Name { get { return name; } set { name = value; } } string code; publicstring Code { get { return code; } set { code = value; } } int creationDate; publicint CreationDate { get { return creationDate; } set { creationDate = value; } } string creator; publicstring Creator { get { return creator; } set { creator = value; } } int modificationDate; publicint ModificationDate { get { return modificationDate; } set { modificationDate = value; } } string modifier; publicstring Modifier { get { return modifier; } set { modifier = value; } } IList<ColumnInfo> columns; public IList<ColumnInfo> Columns { get { return columns; } } publicvoid AddColumn(ColumnInfo mColumn) { if (columns ==null) columns =new List<ColumnInfo>(); columns.Add(mColumn); } } } TableInfo.cs(表資訊類) 程式碼 using System.Collections.Generic;namespace Piggy.Common.PDM { //表資訊publicclass TableInfo { public TableInfo() { } string tableId; publicstring TableId { get { return tableId; } set { tableId = value; } } string objectID; publicstring ObjectID { get { return objectID; } set { objectID = value; } } string name; publicstring Name { get { return name; } set { name = value; } } string code; publicstring Code { get { return code; } set { code = value; } } int creationDate; publicint CreationDate { get { return creationDate; } set { creationDate = value; } } string creator; publicstring Creator { get { return creator; } set { creator = value; } } int modificationDate; publicint ModificationDate { get { return modificationDate; } set { modificationDate = value; } } string modifier; publicstring Modifier { get { return modifier; } set { modifier = value; } } string comment; publicstring Comment { get { return comment; } set { comment = value; } } string physicalOptions; publicstring PhysicalOptions { get { return physicalOptions; } set { physicalOptions = value; } } IList<ColumnInfo> columns; public IList<ColumnInfo> Columns { get { return columns; } } IList<PdmKey> keys; public IList<PdmKey> Keys { get { return keys; } } publicvoid AddColumn(ColumnInfo mColumn) { if (columns ==null) columns =new List<ColumnInfo>(); columns.Add(mColumn); } publicvoid AddKey(PdmKey mKey) { if (keys ==null) keys =new List<PdmKey>(); keys.Add(mKey); } } } PdmReader.cs(pdm檔案讀取器類) 程式碼 using System.Xml;using System.Collections.Generic; using System.Collections; using System; namespace Piggy.Common.PDM { publicclass PdmReader { publicconststring a ="attribute", c ="collection", o ="object"; publicconststring cClasses ="c:Classes"; publicconststring oClass ="o:Class"; publicconststring cAttributes ="c:Attributes"; publicconststring oAttribute ="o:Attribute"; publicconststring cTables ="c:Tables"; publicconststring oTable ="o:Table"; publicconststring cColumns ="c:Columns"; publicconststring oColumn ="o:Column"; XmlDocument xmlDoc; XmlNamespaceManager xmlnsManager; ///<summary>建構函式 </summary>public PdmReader() { // TODO: 在此處新增建構函式邏輯 xmlDoc =new XmlDocument(); } ///<summary>建構函式 </summary>public PdmReader(string pdm_file) { PdmFile = pdm_file; } string pdmFile; publicstring PdmFile { get { return pdmFile; } set { pdmFile = value; if (xmlDoc ==null) { xmlDoc =new XmlDocument(); xmlDoc.Load(pdmFile); xmlnsManager =new XmlNamespaceManager(xmlDoc.NameTable); xmlnsManager.AddNamespace("a", "attribute"); xmlnsManager.AddNamespace("c", "collection"); xmlnsManager.AddNamespace("o", "object"); } } } IList<TableInfo> tables; public IList<TableInfo> Tables { get { return tables; } set { tables = value; } } publicvoid InitData() { if (Tables ==null) Tables =new List<TableInfo>(); XmlNode xnTables = xmlDoc.SelectSingleNode("//"+ cTables, xmlnsManager); foreach (XmlNode xnTable in xnTables.ChildNodes) { Tables.Add(GetTable(xnTable)); } } //初始化"o:Table"的節點private TableInfo GetTable(XmlNode xnTable) { TableInfo mTable =new TableInfo(); XmlElement xe = (XmlElement)xnTable; mTable.TableId = xe.GetAttribute("Id"); XmlNodeList xnTProperty = xe.ChildNodes; foreach (XmlNode xnP in xnTProperty) { switch (xnP.Name) { case"a:ObjectID": mTable.ObjectID = xnP.InnerText; break; case"a:Name": mTable.Name = xnP.InnerText; break; case"a:Code": mTable.Code = xnP.InnerText; break; case"a:CreationDate": mTable.CreationDate = Convert.ToInt32(xnP.InnerText); break; case"a:Creator": mTable.Creator = xnP.InnerText; break; case"a:ModificationDate": mTable.ModificationDate = Convert.ToInt32(xnP.InnerText); break; case"a:Modifier": mTable.Modifier = xnP.InnerText; break; case"a:Comment": mTable.Comment = xnP.InnerText; break; case"a:PhysicalOptions": mTable.PhysicalOptions = xnP.InnerText; break; case"c:Columns": InitColumns(xnP, mTable); break; case"c:Keys": InitKeys(xnP, mTable); break; } } return mTable; |