0

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.

1
  • Please remove "WPF" from the titel, your question has nothing to do with it .. Commented Jun 20, 2012 at 8:38

3 Answers 3

1

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.

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

2 Comments

Thanks Aphelion, if a choice option 2, XSD still in need?
Well somehow the serializer should know how to serialize. You can either manually create the classes using the XML serialization attributes, or you can use the XSD tool to create them for you based on the XSD.
0

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();
    }

Comments

0

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
    }
}

Comments

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.