在我的c#中,每次我要为其他列表编写相同的代码时,我循环遍历XML XElement
列表,只是因为类类型不同而某些列表具有不同的节点名称
这是我的代码,我如何使这个通用为我的所有列表重用相同的功能
if (listType == "A")
{
var lstIndividuals = e.Descendants().Where(c => c.Name.LocalName.ToString() == parentNodeForPerson);
Data _individualData;
List<Data> individualList = new List<Data>();
//here need some generic function to call below functionality
foreach (var individual in lstIndividuals)
{
_individualData = new Data();
_individualData.First_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).First().Value : "";
_individualData.Last_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).First().Value : "";
var lstIndvidualAlias = individual.Descendants().Where(c => c.Name.LocalName.ToString() == childNodeForPerson);
_individualData.Alias_Names = new List<Alias>();
foreach (var alias in lstIndvidualAlias)
{
_individualData.Alias_Names
.Add(new Alias()
{
FirstName = alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).First().Value : "",
LastName = alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).First().Value : ""
});
}
individualList.Add(_individualData);
}
//also needs to check, if child does not exist
var lstEntity = e.Descendants().Where(c => c.Name.LocalName.ToString() == parentNodeForEntity);
Data _entityData;
List<DataEntity> entityList = new List<DataEntity>();
foreach (var entity in lstEntity)
{
_entityData = new Data();
_entityData.First_Name
= entity.Descendants().Where(c => c.Name.LocalName.ToString() == entityParentValue1).Any() == true
? entity.Descendants().Where(c => c.Name.LocalName.ToString() == entityParentValue1).First().Value : "";
_entityData.Last_Name
= entity.Descendants().Where(c => c.Name.LocalName.ToString() == entityParentValue2).Any() == true
? entity.Descendants().Where(c => c.Name.LocalName.ToString() == entityParentValue2).First().Value : "";
var lstEntityAlias = entity.Descendants().Where(c => c.Name.LocalName.ToString() == childNodeForEntity);
_entityData.Alias_Names = new List<Alias>();
foreach (var alias in lstEntityAlias)
{
_entityData.Alias_Names.Add(new Alias(){
FirstName= alias.Descendants().Where(c => c.Name.LocalName.ToString() == entityChildValue1).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == entityChildValue1).First().Value : "",
LastName = alias.Descendants().Where(c => c.Name.LocalName.ToString() == entityChildValue2).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == entityChildValue2).First().Value : ""
});
}
entityList.Add(_entityData);
}
Console.WriteLine(individualList);
Console.WriteLine(entityList);
}
else if (listType == "B")
{
var lstIndividuals = e.Descendants().Where(c => c.Name.LocalName.ToString() == parentNodeForPerson); //nodes can be different
Data _individualData;
List<DataForB> individualList = new List<DataForB>(); //class type type of list is different
foreach (var individual in lstIndividuals)
{
_individualData = new Data();
_individualData.First_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).First().Value : "";
_individualData.Last_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).First().Value : "";
var lstIndvidualAlias = individual.Descendants().Where(c => c.Name.LocalName.ToString() == childNodeForPerson);
_individualData.Alias_Names = new List<Alias>();
foreach (var alias in lstIndvidualAlias)
{
_individualData.Alias_Names.Add(new Alias(){
FirstName =alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).First().Value : "",
LastName = alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).First().Value : ""
});
}
individualList.Add(_individualData);
}
Console.WriteLine(individualList);
}
else if (listType == "C")
{
//this type also repeat the same
}
else if (listType == "D")
{
//this type also repeat the same
}
else
{
Console.WriteLine("Not Found");
Console.ReadKey();
}
我尝试创建一个以list
,nodes[]
和classType<T>
作为参数的方法,但我无法理解如何处理这些内部代码
我的原始尝试:
public static List<XElement> DataMaping(object list, string[,] nodes, type<T>)
{
List<XElement> obj = new List<XElement>();
foreach (var individual in list)
{
individualData = new Data(); //how this Data or some other class will be instantiated here
_individualData.First_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue1).First().Value : "";
_individualData.Last_Name
= individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).Any() == true
? individual.Descendants().Where(c => c.Name.LocalName.ToString() == personParentValue2).First().Value : "";
var lstIndvidualAlias = individual.Descendants().Where(c => c.Name.LocalName.ToString() == childNodeForPerson);
_individualData.Alias_Names = new List<Alias>();
foreach (var alias in lstIndvidualAlias)
{
_individualData.Alias_Names.Add(new Alias(){
FirstName =alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue1).First().Value : "",
LastName = alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).Any() == true
? alias.Descendants().Where(c => c.Name.LocalName.ToString() == personChildValue2).First().Value : ""
});
}
individualList.Add(_individualData);
}
return obj;
}