您好,登錄后才能下訂單哦!
public partial class WebFormClassByEntity : System.Web.UI.Page
{
List<string> list = new List<string>();//存放所有節點名稱
protected void Page_Load(object sender, EventArgs e)
{
//讀取xml的文件路徑
string filePaht = Server.MapPath("~/KJ881101REC_GDXYKJWL_20150519112906795045.xml");
getxml(filePaht, list);
ClaseEntity();
}
/// <summary>
/// 獲取指定路徑下XML的所有節點值
/// </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>
/// 遞歸遍歷所有節點
/// </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)
{
//如果這個節點沒有出現過,則添加到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>
/// 生成實體類
/// </summary>
public void ClaseEntity()
{
//準備一個代碼編譯器單元
CodeCompileUnit unit = new CodeCompileUnit();
//準備必要的命名空間(這個是指要生成的類的空間)
CodeNamespace sampleNamespace = new CodeNamespace("測試_命名空間");//命名空間名稱
//導入必要的命名空間
sampleNamespace.Imports.Add(new CodeNamespaceImport("System"));//引用的命名空間
sampleNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));//引用的命名空間
//準備要生成的類的定義
CodeTypeDeclaration Customerclass = new CodeTypeDeclaration("Customer");//類名稱
//指定這是一個Class
Customerclass.IsClass = true;
Customerclass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Public;
//把這個類放在這個命名空間下
sampleNamespace.Types.Add(Customerclass);
//把該命名空間加入到編譯器單元的命名空間集合中
unit.Namespaces.Add(sampleNamespace);
string outputFile = "E://work//生成實體類程序//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);
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。