C#全新。我正在开发一个ASP MVC应用程序。我将数据导出到XML,所有这些都正常工作。我遇到的问题是我无法弄清楚如何正确输出数据。基本上我是根据ID以及登录用户ID从列中获取数据,例如12。 这个例子根据controllerID和userID拉出3行,数字如下:12,12,11。在我的XML中我希望它显示1 - 35.他们现在的方式是1-12 ,1-12,1-11。数据是正确的,但是我可以从列表中返回的数字,将它们全部加在一起并显示为1-35。 这是代码:
public ActionResult ExportToXML()
{
var lClist = db.LightControllers.Where(x => x.userID == LoggedInUser.id).OrderBy(x => x.controllerID).ToList();
Networks n = new Networks();
n.computer = "computer1";
foreach(var i in lClist)
{
int numU = Convert.ToInt32(i.NumUniverses);
for (int j = 0; j < numU; j++)
{
network netToAdd = new network();
netToAdd.NetworkType = "E131";
netToAdd.ComPort = i.ipaddress;
netToAdd.BaudRate = (j + 1).ToString();
netToAdd.MaxChannels = "510";
n.network.Add(netToAdd);
}
}
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
string path = Server.MapPath("~/XMLFiles/");
string filename = "x_networks.xml";
string filepath = Path.Combine(path, filename);
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
}
XmlSerializer serial = new XmlSerializer(typeof(viewmodels.Networks));
StreamWriter writer = new StreamWriter(filepath);
serial.Serialize(writer, n, ns);
writer.Close();
return File(filepath, "application/xml", filename);
}
以下是当前xml输出的示例。
<?xml version="1.0" encoding="utf-8"?>
<Networks computer="xlights">
<network NetworkType="E131" ComPort="192.168.1.110" BaudRate="1" MaxChannels="510" />
<network NetworkType="E131" ComPort="192.168.1.110" BaudRate="2" MaxChannels="510" />
...
</Networks>
答案 0 :(得分:0)
试试这个:
public ActionResult ExportToXML()
{
var lClist = db.LightControllers.Where(x => x.userID == LoggedInUser.id).OrderBy(x => x.controllerID).ToList();
int baudIterator = 1;
Networks n = new Networks();
n.computer = "computer1";
foreach(var i in lClist)
{
int numU = Convert.ToInt32(i.NumUniverses);
for (int j = 0; j < numU; j++)
{
network netToAdd = new network();
netToAdd.NetworkType = "E131";
netToAdd.ComPort = i.ipaddress;
netToAdd.BaudRate = baudIterator.ToString();
netToAdd.MaxChannels = "510";
n.network.Add(netToAdd);
baudIterator++;
}
}
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
string path = Server.MapPath("~/XMLFiles/");
string filename = "x_networks.xml";
string filepath = Path.Combine(path, filename);
if (System.IO.File.Exists(filepath))
{
System.IO.File.Delete(filepath);
}
XmlSerializer serial = new XmlSerializer(typeof(viewmodels.Networks));
StreamWriter writer = new StreamWriter(filepath);
serial.Serialize(writer, n, ns);
writer.Close();
return File(filepath, "application/xml", filename);
}