0

我需要从 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<>,我该怎么办?

最好的祝福。

4

3 回答 3

1

我认为您的方法不是很安全。您可以选择将所有内容设为强类型并由以下方式定义:

  1. 为 XML 文件创建 XSD
  2. 使用 XSD.EXE 生成可序列化的类
  3. 用于XmlSerializer将 XML 反序列化为类实例
  4. 修改实例
  5. 用于XmlSerializer序列化回 XML

另一种选择是为三个派生类创建一个基类。这样,列表仍然可以将其内容序列化为其派生类型。但是,序列化程序随后将序列化类型信息。

于 2012-06-20T07:47:07.323 回答
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();
    }
于 2014-05-08T21:36:00.680 回答
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
    }
}
于 2015-07-16T13:07:05.320 回答