构建XML

2015-11-23 15:16:47  访问(1593) 赞(0) 踩(0)


        /// <summary>
        /// 构建XML
        /// </summary>
        /// <returns></returns>
        public string InfoToXML()
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                //xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\" ?>");

                //XmlElement root = xmlDoc.DocumentElement;

                //XmlNode xnTableInfo = xmlDoc.CreateElement("TableInfo");
                //root.AppendChild(xnTableInfo);

                //XmlNode xnTableData = xmlDoc.CreateElement("DatabaseName");
                //xnTableData.InnerText = this.DatabaseName;
                //xnTableInfo.AppendChild(xnTableData);

                //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
                XmlDeclaration xmldecl;
                xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
                xmlDoc.AppendChild(xmldecl);

                DataBaseTableName dtn = DataBaseTableName.instance;
                DataBaseColumnName dcn = DataBaseColumnName.instance;


                //加入一个根元素
                XmlElement xmlElem = xmlDoc.CreateElement("", dtn.TB, "");

                xmlElem.SetAttribute(dtn.TableId, this.TableId);
                xmlElem.SetAttribute(dtn.UserId, this.UserId);
                xmlElem.SetAttribute(dtn.TableUser, this.TableUser);
                xmlElem.SetAttribute(dtn.DatabaseName, this.DatabaseName);
                xmlElem.SetAttribute(dtn.TableName, this.TableName);
                xmlElem.SetAttribute(dtn.TableComment, this.TableComment);
                xmlElem.SetAttribute(dtn.CreateTime, this.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                xmlElem.SetAttribute(dtn.UpdateTime, this.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                 
                xmlDoc.AppendChild(xmlElem);

                List<DataBaseTableColumnInfo> dcList 
                    = 
                    getListDataBaseTableColumnInfo();

                if (dcList != null)
                {
                    XmlElement sonElem
                        =
                        null;

                    foreach (DataBaseTableColumnInfo info in dcList)
                    {
                        sonElem = xmlDoc.CreateElement("", dcn.DC, "");

                        sonElem.SetAttribute(dcn.DataColumnId, info.DataColumnId);
                        sonElem.SetAttribute(dcn.DataColumnIndex, info.DataColumnIndex.ToString());
                        sonElem.SetAttribute(dcn.DataColumnName, info.DataColumnName);
                        sonElem.SetAttribute(dcn.DataColumnComment, info.DataColumnComment);
                        sonElem.SetAttribute(dcn.DataColumnIsPrimaryKey, BooleanToStr(info.DataColumnIsPrimaryKey));
                        sonElem.SetAttribute(dcn.DataColumnType, info.DataColumnType);
                        sonElem.SetAttribute(dcn.SqlLength, info.SqlLength.ToString());
                        sonElem.SetAttribute(dcn.SqlSize, info.SqlSize.ToString());
                        sonElem.SetAttribute(dcn.SqlSizeEnd, info.SqlSizeEnd.ToString());
                        sonElem.SetAttribute(dcn.AllowDBNull, BooleanToStr(info.AllowDBNull));
                        sonElem.SetAttribute(dcn.SqlDefaultValue, info.SqlDefaultValue);
                        sonElem.SetAttribute(dcn.IsUnique, BooleanToStr(info.IsUnique));
                        sonElem.SetAttribute(dcn.UniqueName, info.UniqueName);
                        sonElem.SetAttribute(dcn.ForeignKeyTableId, info.ForeignKeyTableId);
                        sonElem.SetAttribute(dcn.ForeignKeyTableUser, info.ForeignKeyTableUser);
                        sonElem.SetAttribute(dcn.ForeignKeyTableName, info.ForeignKeyTableName);
                        sonElem.SetAttribute(dcn.ForeignKeyDataColumnId, info.ForeignKeyDataColumnId);
                        sonElem.SetAttribute(dcn.ForeignKeyDataColumnName, info.ForeignKeyDataColumnName);


                        xmlElem.AppendChild(sonElem);
                    }
                }

                return xmlDoc.InnerXml;
            }
            catch (Exception err)
            {
                return "异常:"+err.Message;
            }

            
        }


标签:构建XML 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)