zoukankan      html  css  js  c++  java
  • C#把Xml转换为DataSet的两种方法

    第1种:

    //通过传入的特定XML字符串,通过 ReadXml函数读取到DataSet中。

      protected static DataSet GetDataSetByXml(string xmlData)
            {
                try
                {
                    DataSet ds = new DataSet();
    
                    using (StringReader xmlSR = new StringReader(xmlData))
                    {
    
                        ds.ReadXml(xmlSR, XmlReadMode.InferTypedSchema); //忽视任何内联架构,从数据推断出强类型架构并加载数据
    
                        if (ds.Tables.Count > 0)
                        {
                            return ds;
                        }
                    }
                    return null;
                }
                catch (Exception)
                {
                    return null;
                }
            }

    第2种:

    /// 通过传入的xml文件路径(含文件名),将格式化的Xml文件自动读取转换为DataSet。

     public static DataSet ConvertXMLFileToDataSet(string xmlFile)
            {
                StringReader stream = null;
                XmlTextReader reader = null;
                try
                {
                    XmlDocument xmld = new XmlDocument();
                    xmld.Load(xmlFile);
    
                    DataSet xmlDS = new DataSet();
                    stream = new StringReader(xmld.InnerXml);
                    //从stream装载到XmlTextReader
                    reader = new XmlTextReader(stream);
                    xmlDS.ReadXml(reader);
                    //xmlDS.ReadXml(xmlFile);
                    return xmlDS;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }
  • 相关阅读:
    链表基础操作2
    数据结构第二章课后作业
    CSAPP第二個實驗bomblab
    链表的基础操作1
    Codeforces 375
    Codeforces 372
    Codeforces 367
    线性同余方程组
    【除草】反演
    【转】组合数求模
  • 原文地址:https://www.cnblogs.com/lampon/p/3158415.html
Copyright © 2011-2022 走看看