从FOR XML PATH拆分字符串

时间:2013-01-07 11:22:03

标签: c# asp.net-mvc-3

我有这段代码:

public class TrainingModuleProgressStateDataModelResults
{
    public IEnumerable<DataModels.Training.TrainingModuleProgressStateDataModel> TrainingModuleProgressStates { get; set; }
} 

public class TrainingModuleProgressStateDataModel
{
    public IEnumerable<UserTrainingPointsDataModel> UserTrainingPoints { get; set; }
}  

我需要把它分成:

public class UserTrainingPointsDataModel
{
    public virtual int InteractionType { get; set; }
    public virtual int Points { get; set; }
    public virtual string Name { get; set; }
}

在我的存储库中,我从RawPoints中的数据库返回:

China Incentive Program,50,6,1|India - Q2 Incentive ,50,6,4|China - Q2 Incentive,50,6,5|India Incentive Program,100,8,3|India - Q2 Incentive ,100,8,4

每个集都由pipe character分隔,然后50 = the points,6是交互类型,而1是trainingmoduleid(我不再需要退出)并且名称很好,名称:)

string RawPoints = row["RawPoints"].ToString();
foreach (var RawPoint in RawPoints)
{
    UserTrainingPointsDataModel.Name = RawPoints.Name;???
}

1 个答案:

答案 0 :(得分:1)

您可以使用下面的String.Split

string rawInput = "China Incentive Program,50,6,1|India - Q2 Incentive ,50,6,4|China -     Q2 Incentive,50,6,5|India Incentive Program,100,8,3|India - Q2 Incentive ,100,8,4";
string[] rawPoints = rawInput.Split(new char[]{'|'});

List<UserTrainingPointsDataModel> points = new List<UserTrainingPointsDataModel>();

foreach(string rawPoint in rawPoints)
{
    string[] enrty = rawPoints.Split(new char[]{','});
    var point = new UserTrainingPointDataModel();
    point.Name = entry[0];
    point.Points = entry[1];
    point.InteractionType = entry[2];

    points.Add(point);  
}