导出有效但未正确显示数据

Export works but is not displaying the data correctly

全新的 C#。我正在开发 ASP MVC 应用程序。我正在将数据导出到 XML 并且所有这些都正常工作。我遇到的问题是我不知道如何正确地输出数据。基本上我是从一个列中获取数据,该列是一个数字,例如基于 ID 的 12 以及登录的用户 ID。 此示例根据 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>

试试这个:

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);
}