Home > Net >  How to make the List in the generic entity class attribute information extracted?
How to make the List in the generic entity class attribute information extracted?

Time:09-19

Purpose is to make a generic class, gm will parse XML file information, and inserted into the database,
Entity class part, include erpin and erpinbody two classes, erpin class represents the warehousing entity class, erpbody said information is put in storage, warehousing information contains multiple data information, for the purpose of this generic class is to parse an XML document information at the same time, the insert part receipt information erpin table, insert erpinbody detailed information to erpinbody class, I made a method R2C, this method can separate erpin and erpinbdoy data and inserted into the database,
Now I'd like to R2C this method into a generic generic method, but the inside of the turn erpinbody after don't know how to use generics to extract data,

Entity class code:
 
[XmlRoot]Public class erpin
{
Public int id {get; set; }
Public string storageid {get; set; }
Public string orderid {get; set; }

[XmlElement (" erpinbody ")]
Public List Erpbodylist {get; set; }
}
[Serializable]
Public class erpinbody
{
[XmlElement]
Public int id {get; set; }
[XmlElement]
Public string matecode {get; set; }
[XmlElement]
Public string matename {get; set; }
[XmlElement]
Public int count {get; set; }
[XmlElement]
Public DateTime storagedate {get; set; }
[XmlElement]
Public int hid the {get; set; }
}


XML code:
 & lt; ? The XML version="1.0" encoding="utf-8"?> 
1
Eid2211 & lt;/storageid>
Oid11 & lt;/orderid>
01 & lt;/storagetype>

2 & lt;/id>
Wl0001 & lt;/matecode>
Material 1 & lt;/matename>
2 & lt;/count>
The 2020-03-10 & lt;/storagedate>


3 & lt;/id>
Wl0001 & lt;/matecode>
Material 1 & lt;/matename>
2 & lt;/count>
The 2020-03-10 & lt;/storagedate>



The general analytic method R2C
 
Private string R2C XML (string)
{
ListErpin model=DESerializerStringToEntity (XML);
Bool bRollback=false;
Bool bResult=model. Insert (a);

if (! BResult)
{
BRollback=true;
}
If (bResult)
{
Foreach (erpinbody body in model. Erpbodylist)
{
if (! BRollback)
{
BResult=body. Insert (a);
}

if (! BResult)
{
BRollback=true;
}
}
}
The Type t=typeof (erpin);
PropertyInfo [] pInfo=t.G etProperties ();
//insert information failure, rollback
If (bRollback)
{
Model. Delete (" id="+ model. Id. The ToString () +" ");

Foreach (erpinbody body in model. Erpbodylist)
{
Body. Delete (" id="+ body. Id. The ToString ());
}

LResult. Add (new T_RESULT () {ID=model. ID. The ToString (), the Result="F"});
}
The else
{
LResult. Add (new T_RESULT () {ID=model. ID. The ToString (), the Result="T"});
}
Return XmlUtil. Serializer
}

CodePudding user response:

I'm not quite understand what you mean,
Because you said gm generics, general is what meaning, I don't know the specific
If it is inherited from a base class that is ok, if you want to in view of the derived class that is very troublesome,
Unless you use reflection, but I think you're splicing string, "id=" + body. Id. The ToString ()
It can't completely through reflection,

CodePudding user response:

//insertion failure information, the rollback 
If (bRollback)
{
Model. Delete (" id="+ model. Id. The ToString () +" ");

Foreach (erpinbody body in model. Erpbodylist)
{
Body. Delete (" id="+ body. Id. The ToString ());
}

LResult. Add (new T_RESULT () {ID=model. ID. The ToString (), the Result="F"});
}
The else
{
LResult. Add (new T_RESULT () {ID=model. ID. The ToString (), the Result="T"});
}
Return XmlUtil. Serializer
Roll back this part can be done alone
  •  Tags:  
  • C#
  • Related