您好,登錄后才能下訂單哦!
public partial class WebFormClassByEntity : System.Web.UI.Page
{
List<string> list = new List<string>();//存放所有節(jié)點(diǎn)名稱
protected void Page_Load(object sender, EventArgs e)
{
//讀取xml的文件路徑
string filePaht = Server.MapPath("~/KJ881101REC_GDXYKJWL_20150519112906795045.xml");
getxml(filePaht, list);
ClaseEntity();
}
/// <summary>
/// 獲取指定路徑下XML的所有節(jié)點(diǎn)值
/// </summary>
public List<string> getxml(string xmlFilePath, List<string> list)
{
try
{
XmlDocument doc = new XmlDocument();
if (File.Exists(xmlFilePath))
{
doc.Load(xmlFilePath);
XmlNodeList xnl = doc.DocumentElement.ChildNodes;
GetAllNodes(xnl, list);
}
else
{
list.Add("Error:");
list.Add("Please make sure the path and file are correct!!");
}
return list;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 遞歸遍歷所有節(jié)點(diǎn)
/// </summary>
/// <param name="nodelist"></param>
/// <param name="listnode"></param>
/// <returns></returns>
public List<string> GetAllNodes(XmlNodeList nodelist, List<string> listnode)
{
foreach (XmlElement element in nodelist)
{
//如果這個(gè)節(jié)點(diǎn)沒有出現(xiàn)過,則添加到list列表
if (!listnode.Contains(element.Name))
{
listnode.Add(element.Name);
}
if (element.ChildNodes[0] is XmlText)
{
continue;
}
else
{
GetAllNodes(element.ChildNodes, listnode);
}
}
return listnode;
}
/// <summary>
/// 生成實(shí)體類
/// </summary>
public void ClaseEntity()
{
//準(zhǔn)備一個(gè)代碼編譯器單元
CodeCompileUnit unit = new CodeCompileUnit();
//準(zhǔn)備必要的命名空間(這個(gè)是指要生成的類的空間)
CodeNamespace sampleNamespace = new CodeNamespace("測(cè)試_命名空間");//命名空間名稱
//導(dǎo)入必要的命名空間
sampleNamespace.Imports.Add(new CodeNamespaceImport("System"));//引用的命名空間
sampleNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));//引用的命名空間
//準(zhǔn)備要生成的類的定義
CodeTypeDeclaration Customerclass = new CodeTypeDeclaration("Customer");//類名稱
//指定這是一個(gè)Class
Customerclass.IsClass = true;
Customerclass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Public;
//把這個(gè)類放在這個(gè)命名空間下
sampleNamespace.Types.Add(Customerclass);
//把該命名空間加入到編譯器單元的命名空間集合中
unit.Namespaces.Add(sampleNamespace);
string outputFile = "E://work//生成實(shí)體類程序//WebApplication1//WebApplication1//Customer.cs";//生成的類存放的路徑 //這是輸出文件
//List<string> list = new List<string>();
//list.Add("id");
//list.Add("name");
foreach (var types in list)
{
//添加字段
CodeMemberField field = new CodeMemberField(typeof(System.String), "_" + types);
field.Attributes = MemberAttributes.Private;
Customerclass.Members.Add(field);
//添加屬性
CodeMemberProperty property = new CodeMemberProperty();
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
property.Name = types;
property.HasGet = true;
property.HasSet = true;
property.Type = new CodeTypeReference(typeof(System.String));
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + types)));
property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + types), new CodePropertySetValueReferenceExpression()));
Customerclass.Members.Add(property);
}
Customerclass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute))));
//生成代碼
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
options.BlankLinesBetweenMembers = true;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputFile))
{
provider.GenerateCodeFromCompileUnit(unit, sw, options);
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。