0

At the minute I am loading the data directly from my xaml.cs file, but I want to now get it to load from an XML file as I have no current knowledge on how to do this I was wondering if people could help me out, these are my current files:

enter code here


        //Create a viewmodel and add some data to it. 
        var viewModel = new MyViewModel();
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "Yes", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Unknown", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "Yes", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });
        viewModel.Items.Add(new Data() { Name = "No", Type = "No", Selected = true });

        //Set the window's datacontext to the ViewModel.  This will make binding work. 
        this.DataContext = viewModel;

    }
}

//This is the ViewModel used to bind data
public class MyViewModel
{
    //This could just be a List<Data> but ObservableCollection<T> will automatically 
    //update UI when items are added or removed from the collection. 
    public ObservableCollection<Data> Items { get; set; }

    public MyViewModel()
    {
        Items = new ObservableCollection<Data>();
    }
}

//Just a sample class to hold the data for the grid. 
//This is the class that is contained in the ObservableColleciton in the ViewModel 
public class Data
{
    public string Name { get; set; }
    public string Type { get; set; }
    public bool Selected { get; set; }
}


//This is an example converter.  It looks to see if the element is set to "Yes"  
//If so, it returns Visibility.Collapsed.  Otherwise, it returns Visibility.Visible. 
public class YesToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value is string)
        {
            var input = (string)value;
            if (string.Equals(input, "Yes", StringComparison.CurrentCultureIgnoreCase))
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

}

2
  • 2
    Please search Google / StackOverflow before posting very common questions like these... stackoverflow.com/questions/8972743/… Commented May 16, 2012 at 13:12
  • I would start by looking into MSDN, and checking out topics related to XML, XLinq, XML Serialization. HTH. Commented May 16, 2012 at 13:47

1 Answer 1

2

You can leverage XLinq To serialize/deserialize from XML data.

For example:

Serialization

    public static String ToXml(ObservableCollection<Data> items)
    {
        try
        {
            XElement _items = new XElement("Root",
                                from item in items()
                                select new XElement("Item",
                                    new XElement("Name", item.OrderId),
                                    new XElement("Type", item.OrderType),
                                    new XElement("Selected", item.Security)
                                    )
                                    );

            return _items.ToString();

        }
        catch (Exception ex)
        {


        }
        return String.Empty;
    }

Deserialization

public static ObservableCollection<Data> FromXml(String data)
{
    ObservableCollection<Data> dataCollection = default(ObservableCollection<Data>);
    try
    {
        XElement _items = XElement.Parse(data);
        var items = _items.Elements("Item").Select(i
            =>
            new Data
            {
                Name = i.Element("Name").Value,
                    Selected = bool.Parse(i.Element("Selected").Value),
                    Type = i.Element("Type").Value,
            }
        ).ToArray();

        if (items != null)
        {
            dataCollection = new ObservableCollection<Data>();
            foreach (var item in dataCollection)
            {
                dataCollection.Add(item);
    }
            return dataCollection;
        }
    }
    catch (Exception e)
    {
    }
    return null;
}

You can use the above function with File.ReadAllText, File.WriteAllText to read/write string into file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the responce, I have decided to go straight for using the SQL database and I am figuring out now how to write the information to it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.