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

    转:https://blog.csdn.net/beyondqd/article/details/6724676

    下面给出两个实现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 GetDataSetByXmlpath(string strXmlPath)
    {
               try
               {
                   DataSet ds = new DataSet();
                   //读取XML到DataSet 

                   StreamReader sr = new StreamReader(strXmlPath, Encoding.Default); 

                   ds.ReadXml(sr); 

                   sr.Close(); 

                   if (ds.Tables.Count > 0)
                       return ds;
                   return null;
               }
               catch (Exception)
               {
                   return null;
               }
    }

  • 相关阅读:
    升级python
    python内置函数整理
    import 搜索路径
    webpy 解决中文出现UnicodeDecodeError: 'ascii' codec can't decode byte 问题
    未预期的符号 `$'{ '' 附近有语法错误
    python引入模块时import与from ... import的区别
    "Native table 'performance_schema'.'session_variables' has the wrong structure") [SQL: "SHOW VARIABLES LIKE 'sql_mode'"]
    git命令
    Redis简介
    MongoDB基本操作
  • 原文地址:https://www.cnblogs.com/janghe/p/9758519.html
Copyright © 2011-2022 走看看