1

I'm trying to produce a xml file with the structure below using C# models:

<root>
  <row>
    <field name="placeholder">Test field 1</field>
    <field name="placeholder">Test field 2</field>
    <field name="placeholder">Test field 3</field>
    <field name="placeholder">Test field 4</field>
    <field name="placeholder">Test field 5</field>
  </row>
  <row>
    <field name="placeholder">Test field 1</field>
    <field name="placeholder">Test field 2</field>
    <field name="placeholder">Test field 3</field>
    <field name="placeholder">Test field 4</field>
    <field name="placeholder">Test field 5</field>
  </row>
</root>

I've set these models up but I'm getting the complete wrong output of what is needed

public class root
{
    public List<row> rows {get; set;}
}

public class row
{
    public List<field> fields {get; set;}
}

public class field
{
    [XmlAttribute("name")]
    public string AttributeName { get; set; }

    [XmlText]
    public string AttributeValue { get; set; }

}

This is the code I've used to create the models

 var root = new root();
 root.rows = new List<row>();

 for (int x = 0; x < 2; x++)
 {
      root.rows.Add(new row()
      {
           fields = new List<field>()
           {
                new field()
                {
                      AttributeName="placeholder",
                      AttributeValue="test1",
                },
                new field()
                {
                      AttributeName="placeholder",
                      AttributeValue="test2",
                },
                new field()
                {
                      AttributeName="placeholder",
                      AttributeValue="test3",
                },
                new field()
                {
                      AttributeName="placeholder",
                      AttributeValue="test4",
                },
                new field()
                {
                      AttributeName="placeholder",
                      AttributeValue="test5",
                }
           }
    });
}

So the root should have multiple rows which can be defined in code but, the rows will have multiple fields just with different attribute names and values.

The new row will will always be added in a loop as there will be data in the loop needed to fill in the field attribute and value.

The problem I'm facing is when I run this I get the ouput below:

<root>
    <rows>
        <row>
            <fields>
                <field name="placeholder">test1</field>
                <field name="placeholder">test2</field>
                <field name="placeholder">test3</field>
                <field name="placeholder">test4</field>
                <field name="placeholder">test5</field>
            </fields>
        </row>
        <row>
            <fields>
                <field name="placeholder">test1</field>
                <field name="placeholder">test2</field>
                <field name="placeholder">test3</field>
                <field name="placeholder">test4</field>
                <field name="placeholder">test5</field>
            </fields>
        </row>
    </rows>
</root>
2
  • And what is the problem? Commented Jan 20, 2021 at 17:17
  • Apologies! Added the problem into the question Commented Jan 20, 2021 at 17:44

1 Answer 1

1

Use following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
         static void Main(string[] args)
         {
             root root = new root()
             {
                 rows = new List<row>() {
                    new row() {
                        fields = new List<field>() {
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 1"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 2"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 3"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 4"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 5"}
                        }
                    },
                    new row() {
                        fields = new List<field>() {
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 1"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 2"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 3"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 4"},
                            new field() { AttributeName = "placeholder", AttributeValue = "Test Field 5"}
                        }
                    }
                }
             };

             XmlWriterSettings settting = new XmlWriterSettings();
             settting.Indent = true;
             XmlWriter writer = XmlWriter.Create(FILENAME, settting);

             XmlSerializer serializer = new XmlSerializer(typeof(root));
             serializer.Serialize(writer, root);
 
  
         }
    }
    public class root
    {
        [XmlElement(ElementName = "row")]
        public List<row> rows { get; set; }
    }

    public class row
    {
        [XmlElement("field")]
        public List<field> fields { get; set; }
    }

    public class field
    {
        [XmlAttribute("name")]
        public string AttributeName { get; set; }

        [XmlText]
        public string AttributeValue { get; set; }

    }
 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.