Search code examples
c#.netxmlfor-loopxml-serialization

C# for loop inside deep XML serialization


I am trying to make a deep nested XML file in which I want to insert some data from another file.

How it looks right now:

  <Documents Count="1">
    <Item DATE="2020-09-01" CREATEDBY="TestUser">
      <Row ITEMID="" ACTIVE="" />
    </Item>
  </Documents>

I want it to look like this:

  <Documents Count="1">
    <Item DATE="2020-09-01" CREATEDBY="TestUser">
      <Row ITEMID="" ACTIVE="" />
      <Row ITEMID="" ACTIVE="" />
      <Row ITEMID="" ACTIVE="" />
      <Row ITEMID="" ACTIVE="" />
    </Item>
  </Documents>

And the problem is that the node "Item" has to have many rows inside of it (node "Row"), and I want to fill data for it from another file by reading it's every line and to do that I need a for loop. I am stuck, because it won't let me add a for loop in there, is there a way to somehow insert a for loop in there?

Thanks.

Here's the code that I have written so far:

class Program
{
    static void Main(string[] args)
    {
        var xmlData = new XmlData();

        xmlData.documents.documentsItem.Add(new DocumentsItem()
        {
            DATE = DateTime.Now.ToString("yyyy-MM-dd"),
            CREATEDBY = "TestUser",

            documentsRow = new List<DocumentsRow>()
            {
            //<-- I want to insert a 'for' loop in here, but it won't allow me to do that
                new DocumentsRow()
                {
                    ITEMID = "",
                    ACTIVE = "",
                }
            }
            });
        xmlData.documents.Count = xmlData.documents.documentsItem.Count;

        using (StreamWriter sw = new StreamWriter(@"c:\a\test.xml", false, Encoding.UTF8))
        {
            new XmlSerializer(typeof(XmlData)).Serialize(sw, xmlData);
        }

    }
}


public class XmlData
{
    [XmlElement("Documents")]
    public Documents documents = new Documents();

    [XmlIgnore]
    public DocumentsRow documentsRow = new DocumentsRow();
}


public class Documents
{
    public Documents()
    {
        documentsItem = new List<DocumentsItem>();
    }

    [XmlAttribute]
    public int Count { get; set; }

    [XmlElement("Item")]
    public List<DocumentsItem> documentsItem { get; set; }
}

public class DocumentsItem
{
    public DocumentsItem()
    {
        documentsRow = new List<DocumentsRow>();
    }

    [XmlAttribute]
    public string DATE { get; set; }

    [XmlAttribute]
    public string CREATEDBY { get; set; }

    [XmlElement("Row")]
    public List<DocumentsRow> documentsRow { get; set; }
}

public class DocumentsRow
{
    [XmlAttribute]
    public string ITEMID { get; set; }

    [XmlAttribute]
    public string ACTIVE { get; set; }
}

Solution

  • using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Xml.Serialization;
    
    namespace ConsoleApp31
    {
        class Program
        {
            static void Main(string[] args)
            {
                var xmlData = new XmlData();
    
                xmlData.documents.documentsItem.Add(new DocumentsItem()
                {
                    DATE = DateTime.Now.ToString("yyyy-MM-dd"),
                    CREATEDBY = "TestUser",
    
                    documentsRow = GetDocuments(100)
                });
                xmlData.documents.Count = xmlData.documents.documentsItem.Count;
    
                using (StreamWriter sw = new StreamWriter(@"c:\a\test.xml", false, Encoding.UTF8))
                {
                    new XmlSerializer(typeof(XmlData)).Serialize(sw, xmlData);
                }
    
            }
            private static List<DocumentsRow> GetDocuments(int number)
            {
                var documents = new List<DocumentsRow>();
                for (int i = 0; i <= number; i++)
                {
                    documents.Add(new DocumentsRow() { ITEMID = "", ACTIVE = "" });
                }
                return documents;
            }
        }
        public class XmlData
        {
            [XmlElement("Documents")]
            public Documents documents = new Documents();
    
            [XmlIgnore]
            public DocumentsRow documentsRow = new DocumentsRow();
        }
    
    
        public class Documents
        {
            public Documents()
            {
                documentsItem = new List<DocumentsItem>();
            }
    
            [XmlAttribute]
            public int Count { get; set; }
    
            [XmlElement("Item")]
            public List<DocumentsItem> documentsItem { get; set; }
        }
    
        public class DocumentsItem
        {
            public DocumentsItem()
            {
                documentsRow = new List<DocumentsRow>();
            }
    
            [XmlAttribute]
            public string DATE { get; set; }
    
            [XmlAttribute]
            public string CREATEDBY { get; set; }
    
            [XmlElement("Row")]
            public List<DocumentsRow> documentsRow { get; set; }
        }
    
        public class DocumentsRow
        {
            [XmlAttribute]
            public string ITEMID { get; set; }
    
            [XmlAttribute]
            public string ACTIVE { get; set; }
        }
    }