WPF从XML读取数据到列表<>

时间:2012-06-20 07:39:52

标签: xml

我需要将数据从XML读取到List<>。 XML文件包含三种表格格式 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 dsProfile = new DataSet();
dsProfile.ReadXml(strProfile);

通过这种方式,三个数据表将自动在dsprofile中。 如何使用列表作为容器来保存XML文件数据? 如果只有一种表格格式,我可以这样做:

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

但是文件中有3种类型的表。如果我仍然想使用List&lt;&gt;?

,我该怎么办?

最诚挚的问候。

3 个答案:

答案 0 :(得分:1)

我认为你的方法不是很安全。您可以选择将所有内容强化并定义为:

  1. 为XML文件创建XSD
  2. 使用XSD.EXE生成可序列化的类
  3. 使用XmlSerializer将XML反序列化为类实例
  4. 修改实例
  5. 使用XmlSerializer序列化回XML
  6. 另一种选择是为三个派生类创建一个基类。这样,列表仍然可以将其内容序列化为派生类型。但是,序列化程序将序列化类型信息。

答案 1 :(得分:0)

你可以使用这个例子

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

答案 2 :(得分:0)

试试这个:

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

viewModelBase.cs文件

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