1

I'm writing a WPF client for an online browser game as a learning project for C#. I want to import the data that is shown on the website, store it in a model and display it through binding. Usually, i go about it this way:

Retreive data with HttpWebRequest, perform Regex to receive the wanted data, and then store it for further use in the Models. With this particular item, a list of cars and their properties in a table, i need 6 properties of the cars, which i can retrieve with 1 Regex query:

var carInfos = Regex.Matches(HTMLBody, @"(?<== }"">).*(?=</td>)");

I get 42 matches from that string, which means there are 7 CarModels to be filled. Now, if i want to fill the models i would go about it this way:

            Cars.AllCars.Add(new CarModel{
                                             Plate = carInfos[0].Value,
                                             Type = carInfos[1].Value,
                                             NetWorth = int.Parse(carInfos[3].Value,
                                             etc, etc..

                                         });

This is my CarModel:

  public class CarModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    private string _plate;

    public string Plate{
        get { return _plate; }
        set{
            if (value == _plate) return;
            _plate = value;
            OnPropertyChanged();
        }
    public string Type { get; set; }
    public int NetWorth { get; set; }
    public State CurrentWorth { get; set; }
    public State Location { get; set; }
    public int Condition { get; set; }
    public int Issues { get; set; }

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

How can i do this dynamically? Every 6th property is the car type, location etc. I want to be able to bind hundreds of cars eventually.

Let's say, for instance, i get 12 matches from the Regex, i.e. two cars. the carInfos[0].value would be a Plate, and the carInfos[6].value would also contain a plate. How can i loop through the results in such a manner that these models will be filled accordingly?

4

1 回答 1

2

This should work for you then -

for (int index = 0; index < carInfos.Count/6; index++)
{
    int offest = index * 6;
    Cars.AllCars.Add(new CarModel{
                                 Plate = carInfos[0 + offest].Value,
                                 Type = carInfos[1 + offest].Value,
                                 NetWorth = int.Parse(carInfos[3 + offest].Value,
                                 etc, etc..    
                                 });
}

Since you have total of 7 items that's why (42/6) = 7 and item is at offset of multiple of 6 hence the logic for calculation of offset (offest = index * 6)

于 2013-03-30T15:14:34.957 回答