一堆这样的格式的XML文件
<AddEx>
   <FileName>1</FileName>
   <FilePath>2</FilePath>
   <Reason>3</Reason>
   <Re>4</Re>
</AddEx>外部函数一个一个传XML路径mFilePath
有一个DataTable pDataTable;
pDataTable对应XML文件创建4列
怎么样才能把这四列的值加到pDataTable.Rows里面
还有外面函数传过来的路径是一个一个传的  我的想法是传过来一个XML文件就把他分解  读取其中的
数据加到DataTable里去 然后再第二个XMl文件传过来分解 也加到同一个表中,结束后在把表返回去分不够可以加!!!!

解决方案 »

  1.   

    给段代码 你参考下
    public static DataTable GenerateDataTableByGrdMapping(string logicName)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("ColumnType", typeof(byte));
    dt.Columns.Add("Frozen", typeof(bool));
    dt.Columns.Add("Simplified", typeof(string));
    dt.Columns.Add("Tranditional", typeof(string));
    dt.Columns.Add("English", typeof(string));
    dt.Columns.Add("Width", typeof(int));
    dt.Columns.Add("DataPropertyName", typeof(string));
    dt.Columns.Add("Format", typeof(string));
    dt.Columns.Add("NullValue", typeof(string));
    dt.Columns.Add("Resizable", typeof(byte));
    dt.Columns.Add("SortMode", typeof(byte));
    dt.Columns.Add("ReadOnly", typeof(bool));
    dt.Columns.Add("AutoSizeMode", typeof(byte));
    dt.Columns.Add("FillWeight", typeof(int));
    dt.Columns.Add("AlignMent", typeof(byte)); XmlDocument doc = new XmlDocument(); string path = HttpContext.Current == null ? ConfigurationManager.AppSettings["GridViewDisplayMapping"] : HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GridViewDisplayMapping"]); using(XmlReader xmlReader = XmlReader.Create(path))
    {
    while(xmlReader.Read())
    {
    XmlNode node = doc.ReadNode(xmlReader); if(node.ChildNodes.Count == 0)
    continue; foreach(XmlNode childNode in node.ChildNodes)
    {
    if(childNode.Attributes == null)
    continue; if(childNode.Attributes["logicName"].Value == logicName)
    {
    foreach(XmlNode childNode2 in childNode.ChildNodes)
    {
    if(childNode2.Attributes == null)
    continue; DataRow dr = dt.NewRow();
    dr["Name"] = childNode2.Attributes["name"].Value;
    dr["ColumnType"] = childNode2.Attributes["columnType"].Value;
    dr["Frozen"] = childNode2.Attributes["frozen"].Value;
    dr["Simplified"] = childNode2.Attributes["simplified"].Value;
    dr["Tranditional"] = childNode2.Attributes["tranditional"].Value;
    dr["English"] = childNode2.Attributes["english"].Value;
    dr["Width"] = childNode2.Attributes["width"].Value;
    dr["DataPropertyName"] = childNode2.Attributes["dataPropertyName"].Value;
    dr["Format"] = childNode2.Attributes["format"].Value;
    dr["NullValue"] = childNode2.Attributes["nullValue"].Value;
    dr["Resizable"] = childNode2.Attributes["resizable"].Value;
    dr["SortMode"] = childNode2.Attributes["sortMode"].Value;
    dr["ReadOnly"] = childNode2.Attributes["readOnly"].Value;
    dr["AutoSizeMode"] = childNode2.Attributes["autoSizeMode"].Value;
    dr["FillWeight"] = childNode2.Attributes["fillWeight"].Value;
    dr["AlignMent"] = childNode2.Attributes["alignMent"].Value; dt.Rows.Add(dr);
    } break;
    }
    }
    }
    } return dt;
    }xml 文件内容<gridView logicName="SharpRatio">
    <column name="Selected" columnType="2" frozen="False" simplified="选择" tranditional="选择" english="Compare" width="50" dataPropertyName="Selected" format="" nullValue="--" resizable="1" sortMode="1" readOnly="False" autoSizeMode="0" fillWeight="100" alignMent="1"/>
    <column name="JCode" columnType="0" frozen="False" simplified="基金代码" tranditional="基金代碼" english="jCode" width="70" dataPropertyName="jCode" format="" nullValue="--" resizable="1" sortMode="0" readOnly="True" autoSizeMode="7" fillWeight="70" alignMent="0"/>
    <column name="Title" columnType="0" frozen="False" simplified="基金名称" tranditional="基金名稱" english="Title" width="200" dataPropertyName="FundName" format="" nullValue="--" resizable="1" sortMode="0" readOnly="True" autoSizeMode="7" fillWeight="100" alignMent="0"/>
    <column name="XP" columnType="0" frozen="False" simplified="夏普排行" tranditional="夏普排行" english="1" width="200" dataPropertyName="XP" format="N2" nullValue="--" resizable="1" sortMode="0" readOnly="True" autoSizeMode="7" fillWeight="100" alignMent="0"/>
    </gridView>
      

  2.   

    手上做的事情正好有使用dataset及hash table讀取XML這一塊,可以一起學習下.. 
      

  3.   

    我想问下
    你是打算每次调用一个函数,然后传一个路径然后把这个路径下的xml文件读入到一个datataable中,然后下次再传入一个不同的路径,再存入到一个相同的datatable中,是这样么?
    这种情况可能比较难实现,能不能这些路径统一测存入一个数组中呢?
      

  4.   


      string[] filePaths = new string[] { @"e:\a.xml", @"e:\b.xml" };
               
                DataSet ds = new DataSet();
                for (int i = 0; i < filePaths.Length; i++)
                {
                    ds.ReadXml(filePaths[i]);
                }
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();a.xml<?xml version="1.0" encoding="utf-8"?>
    <AddEx>
      <FileName>1</FileName>
      <FilePath>2</FilePath>
      <Reason>3</Reason>
      <Re>4</Re>
    </AddEx>
    b.xml<?xml version="1.0" encoding="utf-8"?>
    <AddEx>
      <FileName>5</FileName>
      <FilePath>6</FilePath>
      <Reason>7</Reason>
      <Re>8</Re>
    </AddEx>
      

  5.   

    假如我的XML结构中还有几条数据
    <a>7</a>
    <b>8</b>但是我不需要他们   我只需要读出每个XML文件的那四条数据  然后组建出一个新的DataTable表这样该怎么弄
      

  6.   


    <?xml version="1.0" encoding="utf-8"?>
    <AddEx>
      <FileName>5</FileName>
      <FilePath>6</FilePath>
      <Reason>7</Reason>
      <Re>8</Re>
       <a>7</a>
       <b>8</b>
    </AddEx>
    是这个意思么
      

  7.   

    建议你看下怎么读取XML中特点节点的内容
    http://blog.csdn.net/sendling/archive/2007/07/18/1696352.aspxXmlNode root = xmlDoc.SelectSingleNode("FileName");//查找“FileName”节点
    只要XML读正确了,从网上找个DataTable的创建实例就行了。