【发布时间】:2014-04-01 12:46:46
【问题描述】:
我的客户给了我一个webservice,它是用 ColdFusion 编写的,我一直在尝试用 C# 来使用它。我一直在尝试将 XML 转换为 DataTable,但我得到了一个没有行的 DataTable。
这是我的 XML:
<?xml version='1.0' standalone='yes'?>
<dtFirms>
<NR>2</NR>
<NAME>CS Net - 2014</NAME>
<NRTIT>2 - 2014 - CS Net - 2014</NRTIT>
<PERIOD>7</PERIOD>
</dtFirms>
这是我的 C# 代码。它总是返回空的 DataTable。我做错了什么?
public DataTable ReadXmlIntoDataTable(string s) {
//create the DataTable that will hold the data
DataTable table = new DataTable("dtFirms");
try
{
//open the file using a Stream
using (Stream stream = GenerateStreamFromString(s))
{
//create the table with the appropriate column names
table.Columns.Add("NR", typeof(string));
table.Columns.Add("NAME", typeof(string));
table.Columns.Add("NRTIT", typeof(string));
table.Columns.Add("PERIOD", typeof(int));
//use ReadXml to read the XML stream
table.ReadXml(stream);
//return the results
return table;
}
}
catch (Exception ex)
{
return table;
}
}
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
它总是返回空的DataTable。我究竟做错了什么?请帮帮我。
【问题讨论】:
-
您必须读取属性中的每个值并手动将它们转换为所需的值(字符串除外)。