C#中读取pdm文件信息(PowerDesigner读取方法)(源码共享)。
其实PowerDesigner的pdm文件是一个xml文件来的,我们可以当作读取xml文件一样的方式来读pdm文件。
其中,我们需要注意的是:如果我们不使用命名空间管理器的话,系统会提示如下错误:
需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Xml.XPath.XPathException: 需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
因此,我们读取pdm文件代码如下:
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
{
//字段信息
public class ColumnInfo
{
public ColumnInfo()
{ }
string columnId;
public string ColumnId
{
get { return columnId; }
set { columnId = value; }
}
string objectID;
public string ObjectID
{
get { return objectID; }
set { objectID = value; }
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string code;
public string Code
{
get { return code; }
set { code = value; }
}
int creationDate;
public int CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
string creator;
public string Creator
{
get { return creator; }
set { creator = value; }
}
int modificationDate;
public int ModificationDate
{
get { return modificationDate; }
set { modificationDate = value; }
}
string modifier;
public string Modifier
{
get { return modifier; }
set { modifier = value; }
}
string comment;
public string Comment
{
get { return comment; }
set { comment = value; }
}
string dataType;
public string DataType
{
get { return dataType; }
set { dataType = value; }
}
string length;
public string Length
{
get { return length; }
set { length = value; }
}
//是否自增量
bool identity;
public bool Identity
{
get { return identity; }
set { identity = value; }
}
bool mandatory;
//禁止为空
public bool Mandatory
{
get { return mandatory; }
set { mandatory = value; }
}
string extendedAttributesText;
//扩展属性
public string ExtendedAttributesText
{
get { return extendedAttributesText; }
set { extendedAttributesText = value; }
}
string physicalOptions;
public string PhysicalOptions
{
get { return physicalOptions; }
set { physicalOptions = value; }
}
}
}
PdmKey.cs(主键类)
namespace Piggy.Common.PDM
{
public class PdmKey
{
public PdmKey()
{
}
string keyId;
public string KeyId
{
get { return keyId; }
set { keyId = value; }
}
string objectID;
public string ObjectID
{
get { return objectID; }
set { objectID = value; }
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string code;
public string Code
{
get { return code; }
set { code = value; }
}
int creationDate;
public int CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
string creator;
public string Creator
{
get { return creator; }
set { creator = value; }
}
int modificationDate;
public int ModificationDate
{
get { return modificationDate; }
set { modificationDate = value; }
}
string modifier;
public string Modifier
{
get { return modifier; }
set { modifier = value; }
}
IList<ColumnInfo> columns;
public IList<ColumnInfo> Columns
{
get { return columns; }
}
public void AddColumn(ColumnInfo mColumn)
{
if (columns == null)
columns = new List<ColumnInfo>();
columns.Add(mColumn);
}
}
}
TableInfo.cs(表信息类)
namespace Piggy.Common.PDM
{
//表信息
public class TableInfo
{
public TableInfo()
{
}
string tableId;
public string TableId
{
get { return tableId; }
set { tableId = value; }
}
string objectID;
public string ObjectID
{
get { return objectID; }
set { objectID = value; }
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string code;
public string Code
{
get { return code; }
set { code = value; }
}
int creationDate;
public int CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
string creator;
public string Creator
{
get { return creator; }
set { creator = value; }
}
int modificationDate;
public int ModificationDate
{
get { return modificationDate; }
set { modificationDate = value; }
}
string modifier;
public string Modifier
{
get { return modifier; }
set { modifier = value; }
}
string comment;
public string Comment
{
get { return comment; }
set { comment = value; }
}
string physicalOptions;
public string 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; }
}
public void AddColumn(ColumnInfo mColumn)
{
if (columns == null)
columns = new List<ColumnInfo>();
columns.Add(mColumn);
}
public void AddKey(PdmKey mKey)
{
if (keys == null)
keys = new List<PdmKey>();
keys.Add(mKey);
}
}
}
PdmReader.cs(pdm文件读取器类)
using System.Collections.Generic;
using System.Collections;
using System;
namespace Piggy.Common.PDM
{
public class PdmReader
{
public const string a = "attribute", c = "collection", o = "object";
public const string cClasses = "c:Classes";
public const string oClass = "o:Class";
public const string cAttributes = "c:Attributes";
public const string oAttribute = "o:Attribute";
public const string cTables = "c:Tables";
public const string oTable = "o:Table";
public const string cColumns = "c:Columns";
public const string 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;
public string 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; }
}
public void 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;
}
//初始化"c:Columns"的节点
private void InitColumns(XmlNode xnColumns, TableInfo pTable)
{
foreach (XmlNode xnColumn in xnColumns)
{
pTable.AddColumn(GetColumn(xnColumn));
}
}
//初始化c:Keys"的节点
private void InitKeys(XmlNode xnKeys, TableInfo pTable)
{
foreach (XmlNode xnKey in xnKeys)
{
pTable.AddKey(GetKey(xnKey));
}
}
private ColumnInfo GetColumn(XmlNode xnColumn)
{
ColumnInfo mColumn = new ColumnInfo();
XmlElement xe = (XmlElement)xnColumn;
mColumn.ColumnId = xe.GetAttribute("Id");
XmlNodeList xnCProperty = xe.ChildNodes;
foreach (XmlNode xnP in xnCProperty)
{
switch (xnP.Name)
{
case "a:ObjectID": mColumn.ObjectID = xnP.InnerText;
break;
case "a:Name": mColumn.Name= xnP.InnerText;
break;
case "a:Code": mColumn.Code = xnP.InnerText;
break;
case "a:CreationDate": mColumn.CreationDate = Convert.ToInt32(xnP.InnerText);
break;
case "a:Creator": mColumn.Creator = xnP.InnerText;
break;
case "a:ModificationDate": mColumn.ModificationDate = Convert.ToInt32(xnP.InnerText);
break;
case "a:Modifier": mColumn.Modifier = xnP.InnerText;
break;
case "a:Comment": mColumn.Comment = xnP.InnerText;
break;
case "a:DataType": mColumn.DataType= xnP.InnerText;
break;
case "a:Length": mColumn.Length= xnP.InnerText;
break;
case "a:Identity": mColumn.Identity = pgConvert.ConvertStringToBoolean(xnP.InnerText);
break;
case "a:Mandatory": mColumn.Mandatory = pgConvert.ConvertStringToBoolean(xnP.InnerText);
break;
case "a:PhysicalOptions": mColumn.PhysicalOptions= xnP.InnerText;
break;
case "a:ExtendedAttributesText": mColumn.ExtendedAttributesText= xnP.InnerText;
break;
}
}
return mColumn;
}
private PdmKey GetKey(XmlNode xnKey)
{
PdmKey mKey = new PdmKey();
XmlElement xe = (XmlElement)xnKey;
mKey.KeyId = xe.GetAttribute("Id");
XmlNodeList xnKProperty = xe.ChildNodes;
foreach (XmlNode xnP in xnKProperty)
{
switch (xnP.Name)
{
case "a:ObjectID": mKey.ObjectID = xnP.InnerText;
break;
case "a:Name": mKey.Name = xnP.InnerText;
break;
case "a:Code": mKey.Code = xnP.InnerText;
break;
case "a:CreationDate": mKey.CreationDate = Convert.ToInt32(xnP.InnerText);
break;
case "a:Creator": mKey.Creator = xnP.InnerText;
break;
case "a:ModificationDate": mKey.ModificationDate = Convert.ToInt32(xnP.InnerText);
break;
case "a:Modifier": mKey.Modifier = xnP.InnerText;
break;
//还差 <c:Key.Columns>
}
}
return mKey;
}
}
}
使用方法:
mTest.InitData();
this.Text=Convert.ToString(mTest.Tables.Count);
上面是使用这个pdm读取器的一个应用,把读取出来的表数量列出来。这个读取器可能还有某些地方没有调整好,读取出来的表数量好像比实际的要多,有可能是快捷方式引用出来的表也算进去了。有空必须要完善一下才行。
由于这个pdm读取器只是初步成型,能够完成基本的相关表、字段、主键等的读取功能,由于时间与工作关系,还有更多的信息没有读取处理,在这,我开放给大家,希望大家能够不断地完善,并且希望完善的人可以把源码发给我补充上来。我完善过的代码也会在这里不定时更新。日后并提供给大家下载。由于本文是原著,并非转载。转载的朋友请加原文的链接。标明文章出处。
原创作品出自努力偷懒,转载请说明文章出处:http://www.cnblogs.com/kfarvid/