LIU
LIU

Reputation: 315

WPF read data from XML to list<>

I need to read data from XML to a List<>. The XML file contains three table format XML:

<?xml version="1.0" standalone="yes"?>
<Test>
  <Table1>
    <Column_tb1>8</Column_tb1>
  </Table1>
  <Table2>
    <Column_tb2_AA>1</Column_tb2_AA>
    <Column_tb2_BB>11</Column_tb2_BB>
  </Table2>
  <Table2>
    <Column_tb2_AA>2</Column_tb2_AA>
    <Column_tb2_BB>22</Column_tb2_BB>
  </Table2>
  <Table3>
    <Column_tb3_AA>2</Column_tb1_AA>
    <Column_tb3_BB>1</Column_tb1_BB>
    <Column_tb3_CC>25</Column_tb1_CC>
  </Table3>
</Test>

Dataset can read that quite simple,

DataSet dsProfile = new DataSet();
dsProfile.ReadXml(strProfile);

By this way three datatables will in dsprofile automatic. How can use a list as a container to save XML file data? if only one table format, i can do that:

List<Table1> listtable1 = new List<Table1>();
XmlSerializer serializer = new XmlSerializer(typeof(List<Table1>));
FileStream stream = File.OpenWrite("data.XML");
serializer.Serialize(stream, listtable1);

But there are 3 types of tables in file. What can i do if i still want to use List<>?

Best Regards.

Upvotes: 0

Views: 1650

Answers (3)

DeshDeep Singh
DeshDeep Singh

Reputation: 1843

try this:

ViewModelSerializer viewModelData = ViewModelSerializer.Deserialize(path);
            foreach(ViewModelBase view in viewModelData.Views) {
                WidgetList.Add(view);
            }

viewModelBase.cs File

   /// <summary>
    /// Provides common functionality for ViewModel classes
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged, IMVVMDockingProperties
    {
        #region Data

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion // Data

        #region Protected Methods

        /// <summary>
        /// Raises event property changed.
        /// </summary>
        /// <param name="propertyName">An property expression representation.</param>
        protected void OnPropertyChanged<T>(Expression<Func<T>> action)
        {
            var propertyName = GetPropertyName(action);
            OnPropertyChanged(propertyName);
        }

        /// <summary>
        /// Raises event property changed.
        /// </summary>
        /// <param name="propertyName">A property name.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Helper to set dependency property value.
        /// </summary>
        /// <typeparam name="T">Property type</typeparam>
        /// <param name="target">Target Dependency property</param>
        /// <param name="value">Value to set</param>
        /// <param name="changedProperties">argument list on changed property names we going notify about notify</param>
        /// <returns></returns>
        protected virtual bool SetValue<T>(ref T target, T value, params string[] changedProperties)
        {
            if (Object.Equals(target, value))
            {
                return false; // no changes, same value
            }

            target = value;

            foreach (string property in changedProperties)
            {
                OnPropertyChanged(property);
            }

            return true;
        }

        #endregion // Protected Methods

        #region Private Methods

        /// <summary>
        /// Helper method to get Property name from Expression.
        /// </summary>
        /// <typeparam name="T">Generic type.</typeparam>
        /// <param name="action">Expression.</param>
        /// <returns>A property name.</returns>
        private static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            var propertyName = expression.Member.Name;
            return propertyName;
        }

        #endregion // Private Methods

        #region IMVVMDockingProperties Members

        public string TargetName { get; set; }

        #endregion
    }
}

Upvotes: 0

Ricardo Romo
Ricardo Romo

Reputation: 1624

you can use this example

  using (XmlTextReader xmlReader = new XmlTextReader(your file.xml))
      {
          XDocument xdoc = XDocument.Load(xmlReader);
          var programs= from programItem in xdoc.Root.Elements()
                      select new yourclass {
            Id  = Convert.ToInt32( programItem.Attribute("Id").Value),
            value1 = programItem.Attribute("value1").Value,
            value2 = programItem.Attribute("value2").Value
        };

        result = programs.ToList();
    }

Upvotes: 0

Myrtle
Myrtle

Reputation: 5831

I think your approach is not very safe. You can choose to make everything strong-typed and defined by:

  1. Create XSD for the XML file
  2. Use XSD.EXE to generate a serializable class
  3. Use XmlSerializer to deserialize the XML into a class instance
  4. Modify the instance
  5. Use XmlSerializer to serialize back to XML

Another option is to make a base class for the three derived classes. That way the list can still serialize its contents as their derived types. However, the serializer will then serialize type information.

Upvotes: 1

Related Questions