日期:2014-05-18  浏览次数:21070 次

关于xml配置文件读取的问题(我想用datatable)!帮帮忙
我有一个XML配置文件
<AddExConfig>
  <FileExp>
  <FileExpValue Name="文本文件" Code=".TXT"/>
  <FileExpValue Name="SHP" Code=".SHP"/>
  <FileExpValue Name="TIF" Code=".TIF"/>
  <FileExpValue Name="JPG" Code=".JPG"/>
  <FileExpValue Name="TXT" Code=".TXT"/>
  </FileExp>
</AddExConfig>
想用datatable读取他 然后绑定到下拉框
当我打开一个文件后读取他的扩展名 比如.txt的文件,下拉框内容通过配置文件的初始化成中文名 文本文件

谢谢大家帮助 分不够可以加

------解决方案--------------------
using System.Xml;

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\a.txt"); //这个就是你的文本文件的路径。

XmlNodeList Nodes = doc.SelectNodes("AddExConfig/FileExp/FileExpValue");

foreach (XmlNode node in Nodes)
{
comboBox1.Items.Add(node.Attributes[1].Value.ToString());
}
------解决方案--------------------
XmlDocument _Doc = new XmlDocument();

_Doc.Load(@"D:\temp\file2.xml");



XmlNodeList _NodeList = _Doc.SelectNodes("//AddExConfig/FileExp/FileExpValue");


DataTable _Table = new DataTable();
_Table.Columns.Add("Name");
_Table.Columns.Add("Code");

foreach (XmlNode _Node in _NodeList)
{
_Table.Rows.Add(new object[] { _Node.Attributes["Name"].Value, _Node.Attributes["Code"].Value });
}

comboBox1.DataSource = _Table;
comboBox1.DisplayMember = "Name";

这样看看
------解决方案--------------------
public DataSet CXmlFileToDataSet(string xmlFilePath)
{
if (!string.IsNullOrEmpty(xmlFilePath))
{
string path = xmlFilePath;

StringReader StrStream = null;

XmlTextReader Xmlrdr = null;

try
{
XmlDocument xmldoc = new XmlDocument(); //根据地址加载Xml文件

xmldoc.Load(path);

DataSet ds = new DataSet(); //读取文件中的字符流

StrStream = new StringReader(xmldoc.InnerXml); //获取StrStream中的数据

Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据

ds.ReadXml(Xmlrdr);

return ds;
}
catch (Exception e)
{
throw e;
}
finally
{
//释放资源
if (Xmlrdr != null)
{
Xmlrdr.Close();

StrStream.Close();

StrStream.Dispose();
}
}
}
else
{
return null;
}
}

使用方法:
DataSet ds = DataSet CXmlFileToDataSet(xmlFilePath)
DataTable tb = ds.Tables[0];
OK了吧!
------解决方案--------------------
探讨
如果用datatable.readxml()方法可以吗,怎么用?