locked
Export data to xml File from Controller? RRS feed

  • Question

  • User-729601932 posted

    First of all, I have to say that I am not a programmer and that I stumble through the code more than I walk.
    So please bear with me.

    I want to export data to an xml file.
    To display the data, they are generated in the Controller and displayed in a View.
    That works so far without any problems.
    But now I have to export this data to an xml file.
    I started trying that in the Controller. Creating the file with the correct structure already works.
    Now I have the problem how to read/generate the data directly in the Controller or write it to my file.

    In the View I do this with: "@foreach (var item in Model)"
    But that doesn't work in the Controller.
    I guess it's very easy, but I can't figure it out.
    Can anyone tell me how I can solve this?
    Is the Controller the right place for this or do I have to approach it completely differently?

    Here is the code from my Controller.

    public async Task<IActionResult> Orders(DateTime? startdate, DateTime? enddate, string src_kunde, string src_stadt, int src_order)
    {
        ViewData["Message"] = "WooCommerce";
        ViewBag.startdate = startdate;
        ViewBag.enddate = enddate;
        ViewBag.src_kunde = src_kunde;
        ViewBag.src_stadt = src_stadt;
        ViewBag.src_order = src_order;
    
        RestAPI rest = new RestAPI("https://xxx.com/wp-json/wc/v3/", "ck_xxx", "cs_xxx");
        WCObject wc = new WCObject(rest);
    
        var orders = await wc.Order.GetAll(dic);
    
        if (startdate != null && enddate == null)...
    
        if (startdate == null && enddate != null)...
    
        if (startdate != null && enddate != null)...
    
        if (src_kunde != null)...
    
        // create DataSet
        DataSet ds = CreateDynamicDataSet();
        ds.WriteXml(@"C:\Temp\Orders.xml");
        return View(orders);
    }
    
    
    private DataSet CreateDynamicDataSet()
    {
        DataSet ds = new DataSet("ERKLAERUNGS_UEBERMITTLUNG");
    
        // <INFO_DATEN>
        DataTable INFO_DATEN = new DataTable("INFO_DATEN");
        INFO_DATEN.Columns.Add(new DataColumn("ART_IDENTIFIKATIONSBEGRIFF", Type.GetType("System.String")));
        INFO_DATEN.Columns.Add(new DataColumn("IDENTIFIKATIONSBEGRIFF", Type.GetType("System.String")));
        INFO_DATEN.Columns.Add(new DataColumn("PAKET_NR", Type.GetType("System.String")));
        INFO_DATEN.Columns.Add(new DataColumn("DATUM_ERSTELLUNG", Type.GetType("System.String")));
        INFO_DATEN.Columns.Add(new DataColumn("UHRZEIT_ERSTELLUNG", Type.GetType("System.String")));
        INFO_DATEN.Columns.Add(new DataColumn("ANZAHL_ERKLAERUNGEN", Type.GetType("System.String")));
        ds.Tables.Add(INFO_DATEN);
    
        // <ERKLAERUNG art="U13">
        DataTable ERKLAERUNG = new DataTable("ERKLAERUNG");
        ERKLAERUNG.Columns.Add(new DataColumn("CatID", Type.GetType("System.Int32")));
        ERKLAERUNG.Columns.Add(new DataColumn("SATZNR", Type.GetType("System.Int32")));
        ERKLAERUNG.PrimaryKey = new DataColumn[] { ERKLAERUNG.Columns["CatID"] };
        ds.Tables.Add(ERKLAERUNG);
    
        // <ALLGEMEINE_DATEN>
        DataTable ALLGEMEINE_DATEN = new DataTable("ALLGEMEINE_DATEN");
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("ANBRINGEN", Type.GetType("System.String")));
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("ZRVON", Type.GetType("System.String")));
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("ZRBIS", Type.GetType("System.String")));
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("FASTNR", Type.GetType("System.String")));
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("KUNDENINFO", Type.GetType("System.String")));
        ALLGEMEINE_DATEN.Columns.Add(new DataColumn("CatId", Type.GetType("System.Int32")));
        ds.Tables.Add(ALLGEMEINE_DATEN);
    
        // <ZM>
        DataTable ZM = new DataTable("ZM");
        ZM.Columns.Add(new DataColumn("UID_MS", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("SUM_BGL", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("DREIECK", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("SOLEI", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("KLAG", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("UID_UE", Type.GetType("System.String")));
        ZM.Columns.Add(new DataColumn("CatId", Type.GetType("System.Int32")));
        ds.Tables.Add(ZM);
    
        // <GESAMTRUECKZIEHUNG>
        DataTable GESAMTRUECKZIEHUNG = new DataTable("GESAMTRUECKZIEHUNG");
        GESAMTRUECKZIEHUNG.Columns.Add(new DataColumn("GESAMTRUECK", Type.GetType("System.String")));
        GESAMTRUECKZIEHUNG.Columns.Add(new DataColumn("CatId", Type.GetType("System.Int32")));
        ds.Tables.Add(GESAMTRUECKZIEHUNG);
    
    
        //add relations to the tables here
        ds.Relations.Add("CatSubCat", ERKLAERUNG.Columns["CatID"], ALLGEMEINE_DATEN.Columns["CatID"]);
        ds.Relations["CatSubCat"].Nested = true;
        ds.Relations.Add("CatSubCat2", ERKLAERUNG.Columns["CatID"], ZM.Columns["CatID"]);
        ds.Relations["CatSubCat2"].Nested = true;
        ds.Relations.Add("CatSubCat3", ERKLAERUNG.Columns["CatID"], GESAMTRUECKZIEHUNG.Columns["CatID"]);
        ds.Relations["CatSubCat3"].Nested = true;
    
        DataRow newRow;
    // HERE I HAVE TO INSERT THE DATA // <INFO_DATEN> newRow = INFO_DATEN.NewRow(); newRow["ART_IDENTIFIKATIONSBEGRIFF"] = "FASTNR"; newRow["IDENTIFIKATIONSBEGRIFF"] = "989999999"; newRow["PAKET_NR"] = "999999999"; newRow["DATUM_ERSTELLUNG"] = "JJJJ-MM-TT"; newRow["UHRZEIT_ERSTELLUNG"] = "HH:MM:SS"; newRow["ANZAHL_ERKLAERUNGEN"] = "12"; INFO_DATEN.Rows.Add(newRow); // <ERKLAERUNG art="U13"> newRow = ERKLAERUNG.NewRow(); newRow["CatID"] = "1"; newRow["SATZNR"] = "1234565432"; ERKLAERUNG.Rows.Add(newRow); // <ALLGEMEINE_DATEN> newRow = ALLGEMEINE_DATEN.NewRow(); newRow["ANBRINGEN"] = "U13"; newRow["ZRVON"] = "JJJJ-MM"; newRow["ZRBIS"] = "JJJJ-MM"; newRow["FASTNR"] = "989999999"; newRow["KUNDENINFO"] = "some text"; newRow["CatID"] = "1"; ALLGEMEINE_DATEN.Rows.Add(newRow); // <ZM>
    // HERE I NEED A LOOP LIKE @foreach (var in item in Model) newRow = ZM.NewRow(); newRow["UID_MS"] = "DE11111112511"; newRow["SUM_BGL"] = "-9999999999"; newRow["DREIECK"] = "1"; newRow["SOLEI"] = "1"; newRow["KLAG"] = "1"; newRow["UID_UE"] = "DE11111112511"; newRow["CatID"] = "1"; ZM.Rows.Add(newRow); newRow = ZM.NewRow(); newRow["CatID"] = "1"; newRow["UID_MS"] = "DE26790342511"; newRow["SUM_BGL"] = "123.34"; newRow["DREIECK"] = "1"; newRow["SOLEI"] = "1"; newRow["KLAG"] = "1"; newRow["UID_UE"] = "DE11111112511"; ZM.Rows.Add(newRow);
    // END OF LOOP // <GESAMTRUECKZIEHUNG> newRow = GESAMTRUECKZIEHUNG.NewRow(); newRow["CatID"] = "1"; newRow["GESAMTRUECK"] = "J"; GESAMTRUECKZIEHUNG.Rows.Add(newRow); ds.AcceptChanges(); return ds; }

    Hope that anyone understand my problem an can help me with this.

    Sunday, August 9, 2020 11:25 PM

All replies

  • User711641945 posted

    Hi Paul_412,

    In the View I do this with: "@foreach (var item in Model)"
    But that doesn't work in the Controller.
    I guess it's very easy, but I can't figure it out.

    For foreach loop,you need know what is the data you want to loop.From your code,it seems you may loop the `orders` you get from the api:

    var orders = await wc.Order.GetAll(dic);

    Here is a simple example:

    List<Order> order= new List<Order> { new Order{ CatId = 1, DREIECK = "fsfa", KLAG = "ahfjha", SOLEI = "akfja", SUM_BGL = "jfj", UID_MS = "ahf", UID_UE = "faj" } };
    foreach(var item in order)
    {
         newRow = ZM.NewRow();
         newRow["UID_MS"] =item.UID_MS;
         newRow["SUM_BGL"] = item.SUM_BGL;
         newRow["DREIECK"] = item.DREIECK;
         newRow["SOLEI"] = item.SOLEI;
         newRow["KLAG"] = item.KLAG;
         newRow["UID_UE"] = item.UID_UE;
         newRow["CatID"] = item.CatId;
         ZM.Rows.Add(newRow);
    }

    Best Regards,

    Rena

    Monday, August 10, 2020 7:30 AM
  • User-729601932 posted

    You are right: 'orders' from wc.api is what I need.

    OK - One step further.
    When I enter the values in new Order, at least one entry will be created for me.

    List<Order> order= new List<Order> { new Order{ total = "124.4", UID_UE = "DE145624465faj", value1 = "", value2 = "", value3 = "" } };
    foreach(var item in order)
    {
         newRow = ZM.NewRow();
         newRow["UID_MS"] = "abdf125";
         newRow["SUM_BGL"] = item.total;
         newRow["DREIECK"] = "belongs to 3 different values";
         newRow["SOLEI"] = "abdf125";
         newRow["KLAG"] = "abdf125";
         newRow["UID_UE"] = item.UID_UE;
         newRow["CatID"] = "1";
         ZM.Rows.Add(newRow);
    }

    But this is only the entry that I entered myself.
    How do I get all relevant entries in new Order?

    Tuesday, August 11, 2020 12:52 PM
  • User711641945 posted

    Hi Paul_412,

    How do I get all relevant entries in new Order?

    It depends on you.And your code below seems did what you want:

    var orders = await wc.Order.GetAll(dic);

    And you could just loop it like below:

    foreach(var item in orders)
    {
         newRow = ZM.NewRow();
         newRow["UID_MS"] =item.UID_MS;
         newRow["SUM_BGL"] = item.SUM_BGL;
         newRow["DREIECK"] = item.DREIECK;
         newRow["SOLEI"] = item.SOLEI;
         newRow["KLAG"] = item.KLAG;
         newRow["UID_UE"] = item.UID_UE;
         newRow["CatID"] = item.CatId;
         ZM.Rows.Add(newRow);
    }

    Best Regards,

    Rena

    Wednesday, August 12, 2020 1:59 AM
  • User-729601932 posted

    But this is still my problem.

    When I have this code.

    public async Task<IActionResult> Orders(DateTime? startdate, DateTime? enddate, string src_kunde, string src_stadt, int src_order)
    {
        ...
        var orders = await wc.Order.GetAll(dic);
        ...
    
        // create DataSet
        DataSet ds = CreateDynamicDataSet();
        ds.WriteXml(@"C:\Temp\Orders.xml");
        return View(orders);
    }

    And

            private DataSet CreateDynamicDataSet()
            {
    
                ...
                foreach (var item in orders)
                {
                    newRow = ZM.NewRow();
                    newRow["UID_MS"] = item.total_tax;
                    newRow["SUM_BGL"] = item.total;
                    newRow["DREIECK"] = item.total_tax;
                    newRow["SOLEI"] = item.total_tax;
                    newRow["KLAG"] = item.total_tax;
                    newRow["UID_UE"] = item.total_tax;
                    newRow["CatID"] = "1";
                    ZM.Rows.Add(newRow);
                }
    
            }

    I got the error message
    "the name "orders" does not exist in the current context.

    If I use this code:

           private DataSet CreateDynamicDataSet()
            {
                DataSet ds = new DataSet("ERKLAERUNGS_UEBERMITTLUNG");
    
    
                var orders = await wc.Order.GetAll(dic);
    
    
                foreach (var item in orders)
                {
                    newRow = ZM.NewRow();
                    newRow["UID_MS"] = item.total_tax;
                    newRow["SUM_BGL"] = item.total;
                    newRow["DREIECK"] = item.total_tax;
                    newRow["SOLEI"] = item.total_tax;
                    newRow["KLAG"] = item.total_tax;
                    newRow["UID_UE"] = item.total_tax;
                    newRow["CatID"] = "1";
                    ZM.Rows.Add(newRow);
                }
    
            }

    I got the error message:
    "the await operator can only be used in an async method".

    As already mentioned at the beginning, I am not a programmer and I don´t always understand everything that net.core wants from me.

    Be patient with me and tell me how I can solve this problem.

    Regards
    Paul

    Sunday, August 16, 2020 4:16 PM