前端之家收集整理的这篇文章主要介绍了
xml to datatable,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
xml:
<DocumentElement>
<cityconfig>
<city>阿坝州</city>
<jc>abazhou</jc>
<nbf>s</nbf>
<viewname>view_news_title_index_abazhou</viewname>
</cityconfig>
<cityconfig>
<city>阿克苏</city>
<jc>akesu</jc>
<nbf>s</nbf>
<viewname>view_news_title_index_akesu</viewname>
</cityconfig>
<cityconfig>
<city>安达</city>
<jc>anda</jc>
<nbf>n</nbf>
<viewname>view_news_title_index_anda</viewname>
</cityconfig>
</DocumentElement>
实现方式:
string path = "./XMLFile1.xml"; ;
string content = GetContent(path);
StringReader txtReader = new StringReader(content);
XmlTextReader xmlReader = new XmlTextReader(txtReader);
DataSet ds = new DataSet();
ds.ReadXml(xmlReader);
string m = "";
foreach (DataColumn c in ds.Tables[0].Columns)
{
m += c.ColumnName + "-";
}
return;// ds.Tables[0];
调用方法:
public static String GetContent(String vFileName)
{
StreamReader reader = new StreamReader(vFileName);
StringBuilder result = new StringBuilder();
string line = reader.ReadLine();
while (line != null)
{
result.AppendLine(line);
line = reader.ReadLine();
}
reader.Close();
return result.ToString();
}