How do I represent XML as classes when it has multiple elements of same name












2















Assuming we have the following xml.



<Company>
<Tables>
<Agri>
<Tables>
<Table Id="1">
</Table>
</Tables>
<Tables>
<Table Id="2">
</Table>
</Tables>
</Agri>
<Tables>
<Table Id="3">
</Table>
</Tables>
<Tables>
<Table Id="4">
</Table>
</Tables>
</Tables>
</Company>


How do I represent such xml into classes?



I've tried something like this but doesn't seem to work.



void Test()
{
string xml = //...
using (var reader = new StringReader(xml))
{
var company = (Company)new XmlSerializer(typeof(Company)).Deserialize(reader);
}
}

public class Company
{
public CompanyTables Tables { get; set; }
}
public class CompanyTables
{
[XmlArray]
public Table Tables { get; set; }
}
public class Table
{
[XmlAttribute("Id")]
public string Id { get; set; }
}


Here is what I can get so far.



enter image description here



I was thinking it would populate the CompanyTable.Tables with two Table instances (Id 3 and 4).



I'm ignoring the Agri element for now and just shown here to better reflect the actual structure of the xml.



I'll keep trying but any kind of help is appreciated. Thanks!










share|improve this question

























  • but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

    – Fabio
    Nov 19 '18 at 22:26











  • @Fabio please see updated question.

    – Paolo Go
    Nov 20 '18 at 0:16
















2















Assuming we have the following xml.



<Company>
<Tables>
<Agri>
<Tables>
<Table Id="1">
</Table>
</Tables>
<Tables>
<Table Id="2">
</Table>
</Tables>
</Agri>
<Tables>
<Table Id="3">
</Table>
</Tables>
<Tables>
<Table Id="4">
</Table>
</Tables>
</Tables>
</Company>


How do I represent such xml into classes?



I've tried something like this but doesn't seem to work.



void Test()
{
string xml = //...
using (var reader = new StringReader(xml))
{
var company = (Company)new XmlSerializer(typeof(Company)).Deserialize(reader);
}
}

public class Company
{
public CompanyTables Tables { get; set; }
}
public class CompanyTables
{
[XmlArray]
public Table Tables { get; set; }
}
public class Table
{
[XmlAttribute("Id")]
public string Id { get; set; }
}


Here is what I can get so far.



enter image description here



I was thinking it would populate the CompanyTable.Tables with two Table instances (Id 3 and 4).



I'm ignoring the Agri element for now and just shown here to better reflect the actual structure of the xml.



I'll keep trying but any kind of help is appreciated. Thanks!










share|improve this question

























  • but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

    – Fabio
    Nov 19 '18 at 22:26











  • @Fabio please see updated question.

    – Paolo Go
    Nov 20 '18 at 0:16














2












2








2








Assuming we have the following xml.



<Company>
<Tables>
<Agri>
<Tables>
<Table Id="1">
</Table>
</Tables>
<Tables>
<Table Id="2">
</Table>
</Tables>
</Agri>
<Tables>
<Table Id="3">
</Table>
</Tables>
<Tables>
<Table Id="4">
</Table>
</Tables>
</Tables>
</Company>


How do I represent such xml into classes?



I've tried something like this but doesn't seem to work.



void Test()
{
string xml = //...
using (var reader = new StringReader(xml))
{
var company = (Company)new XmlSerializer(typeof(Company)).Deserialize(reader);
}
}

public class Company
{
public CompanyTables Tables { get; set; }
}
public class CompanyTables
{
[XmlArray]
public Table Tables { get; set; }
}
public class Table
{
[XmlAttribute("Id")]
public string Id { get; set; }
}


Here is what I can get so far.



enter image description here



I was thinking it would populate the CompanyTable.Tables with two Table instances (Id 3 and 4).



I'm ignoring the Agri element for now and just shown here to better reflect the actual structure of the xml.



I'll keep trying but any kind of help is appreciated. Thanks!










share|improve this question
















Assuming we have the following xml.



<Company>
<Tables>
<Agri>
<Tables>
<Table Id="1">
</Table>
</Tables>
<Tables>
<Table Id="2">
</Table>
</Tables>
</Agri>
<Tables>
<Table Id="3">
</Table>
</Tables>
<Tables>
<Table Id="4">
</Table>
</Tables>
</Tables>
</Company>


How do I represent such xml into classes?



I've tried something like this but doesn't seem to work.



void Test()
{
string xml = //...
using (var reader = new StringReader(xml))
{
var company = (Company)new XmlSerializer(typeof(Company)).Deserialize(reader);
}
}

public class Company
{
public CompanyTables Tables { get; set; }
}
public class CompanyTables
{
[XmlArray]
public Table Tables { get; set; }
}
public class Table
{
[XmlAttribute("Id")]
public string Id { get; set; }
}


Here is what I can get so far.



enter image description here



I was thinking it would populate the CompanyTable.Tables with two Table instances (Id 3 and 4).



I'm ignoring the Agri element for now and just shown here to better reflect the actual structure of the xml.



I'll keep trying but any kind of help is appreciated. Thanks!







c# xml xmlserializer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 0:16







Paolo Go

















asked Nov 19 '18 at 22:18









Paolo GoPaolo Go

1,397620




1,397620













  • but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

    – Fabio
    Nov 19 '18 at 22:26











  • @Fabio please see updated question.

    – Paolo Go
    Nov 20 '18 at 0:16



















  • but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

    – Fabio
    Nov 19 '18 at 22:26











  • @Fabio please see updated question.

    – Paolo Go
    Nov 20 '18 at 0:16

















but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

– Fabio
Nov 19 '18 at 22:26





but doesn't seem to work. - can you elaborate what not working, how you check it is not working...

– Fabio
Nov 19 '18 at 22:26













@Fabio please see updated question.

– Paolo Go
Nov 20 '18 at 0:16





@Fabio please see updated question.

– Paolo Go
Nov 20 '18 at 0:16












3 Answers
3






active

oldest

votes


















1














Table class is wrapped within Tables class. So your Tables class should have "internal" collection of itself(Tables)



public class Company
{
public List<Tables> Tables { get; set; }
}

// As you said Agri type ignored for now
public class Tables
{
public Table Table { get; set; } // Use collection if Table can be more the one

[XmlElement("Tables")]
public Tables InnerTables { get; set; }
}

public class Table
{
[XmlAttribute]
public string Id { get; set; }
}


Test



private T DeserialzeFrom<T>(string xml)
{
using (var reader = new StringReader(xml))
{
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
}

[Test]
public void ShouldDeserializeCompany()
{
var xml = // Build xml string;

// Expected deserialized object
var expected = new Company
{
Tables = new List<Tables>
{
new Tables { Table = new Table { Id = "3" }},
new Tables { Table = new Table { Id = "4" }}
}
};

var actual = DeserialzeFrom<Company>(xml);

actual.ShouldBeEquivalentTo(expected); // Test passing
}





share|improve this answer
























  • Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

    – Paolo Go
    Nov 20 '18 at 0:57











  • @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

    – Fabio
    Nov 20 '18 at 1:17











  • I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

    – Paolo Go
    Nov 20 '18 at 1:19



















1














I think this is the class structure you require. With this structure I have serialised the data resulting in a matching XML output. Deserialising your xml above works too.



public class Company
{
public Company()
{
this.Tables = new CompanyTables();
}

[XmlElement]
public CompanyTables Tables { get; set; }
}

public class CompanyTables
{
public CompanyTables()
{
this.Agri = new Agri();
}

[XmlElement]
public TablesArr Tables { get; set; }

[XmlElement]
public Agri Agri { get; set; }
}

public class Agri
{
public Agri() { }

[XmlElementAttribute("Tables")]
public TablesArr Tables { get; set; }
}

public class TablesArr
{
public TablesArr() { }

[XmlElementAttribute("Table")]
public Table Tables { get; set; }
}

public class Table
{
public Table() { }

[XmlAttribute("Id")]
public int Id { get; set; }
}


Serialise test:



string xml = "<Company><Tables><Agri><Tables><Table Id="1"></Table></Tables><Tables><Table Id="2"></Table></Tables></Agri><Tables><Table Id="3"></Table></Tables><Tables><Table Id="4"></Table></Tables></Tables></Company>";

XmlSerializer sx = new XmlSerializer(typeof(Company));

Company company = null;
using (MemoryStream ms = new MemoryStream())
{
ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);

ms.Flush();

ms.Seek(0, SeekOrigin.Begin);

company = (Company)sx.Deserialize(ms);
}


Deserialise Test:



Company company = new TestBed.Company();

company.Tables.Agri.Tables = new TablesArr[2];
company.Tables.Agri.Tables[0] = new TablesArr();
company.Tables.Agri.Tables[0].Tables = new Table[1];
company.Tables.Agri.Tables[0].Tables[0] = new Table() { Id=1 };
company.Tables.Agri.Tables[1] = new TablesArr();
company.Tables.Agri.Tables[1].Tables = new Table[1];
company.Tables.Agri.Tables[1].Tables[0] = new Table() { Id=2 };

company.Tables.Tables = new TablesArr[2];
company.Tables.Tables[0] = new TablesArr();
company.Tables.Tables[0].Tables = new Table[1];
company.Tables.Tables[0].Tables[0] = new TestBed.Table() { Id=3 };

company.Tables.Tables[1] = new TablesArr();
company.Tables.Tables[1].Tables = new Table[1];
company.Tables.Tables[1].Tables[0] = new TestBed.Table() { Id=4 };


XmlSerializer sx = new XmlSerializer(company.GetType());

using (MemoryStream ms = new MemoryStream())
{
sx.Serialize(ms, company);

ms.Seek(0, SeekOrigin.Begin);

Console.WriteLine("[{0}]", Encoding.UTF8.GetString(ms.ToArray()));
}





share|improve this answer

































    0














    Here is what I got to include Agri based on @Fabio's answer.



    public class Company
    {
    public CompanyTables Tables { get; set; }
    }

    public class CompanyTables
    {
    [XmlElement]
    public List<Tables> Tables { get; set; }

    public List<Tables> Agri { get; set; }
    }

    public class Tables
    {
    [XmlElement]
    public List<Table> Table { get; set; }
    }

    public class Table
    {
    [XmlAttribute]
    public int Id { get; set; }
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383461%2fhow-do-i-represent-xml-as-classes-when-it-has-multiple-elements-of-same-name%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Table class is wrapped within Tables class. So your Tables class should have "internal" collection of itself(Tables)



      public class Company
      {
      public List<Tables> Tables { get; set; }
      }

      // As you said Agri type ignored for now
      public class Tables
      {
      public Table Table { get; set; } // Use collection if Table can be more the one

      [XmlElement("Tables")]
      public Tables InnerTables { get; set; }
      }

      public class Table
      {
      [XmlAttribute]
      public string Id { get; set; }
      }


      Test



      private T DeserialzeFrom<T>(string xml)
      {
      using (var reader = new StringReader(xml))
      {
      return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
      }
      }

      [Test]
      public void ShouldDeserializeCompany()
      {
      var xml = // Build xml string;

      // Expected deserialized object
      var expected = new Company
      {
      Tables = new List<Tables>
      {
      new Tables { Table = new Table { Id = "3" }},
      new Tables { Table = new Table { Id = "4" }}
      }
      };

      var actual = DeserialzeFrom<Company>(xml);

      actual.ShouldBeEquivalentTo(expected); // Test passing
      }





      share|improve this answer
























      • Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

        – Paolo Go
        Nov 20 '18 at 0:57











      • @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

        – Fabio
        Nov 20 '18 at 1:17











      • I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

        – Paolo Go
        Nov 20 '18 at 1:19
















      1














      Table class is wrapped within Tables class. So your Tables class should have "internal" collection of itself(Tables)



      public class Company
      {
      public List<Tables> Tables { get; set; }
      }

      // As you said Agri type ignored for now
      public class Tables
      {
      public Table Table { get; set; } // Use collection if Table can be more the one

      [XmlElement("Tables")]
      public Tables InnerTables { get; set; }
      }

      public class Table
      {
      [XmlAttribute]
      public string Id { get; set; }
      }


      Test



      private T DeserialzeFrom<T>(string xml)
      {
      using (var reader = new StringReader(xml))
      {
      return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
      }
      }

      [Test]
      public void ShouldDeserializeCompany()
      {
      var xml = // Build xml string;

      // Expected deserialized object
      var expected = new Company
      {
      Tables = new List<Tables>
      {
      new Tables { Table = new Table { Id = "3" }},
      new Tables { Table = new Table { Id = "4" }}
      }
      };

      var actual = DeserialzeFrom<Company>(xml);

      actual.ShouldBeEquivalentTo(expected); // Test passing
      }





      share|improve this answer
























      • Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

        – Paolo Go
        Nov 20 '18 at 0:57











      • @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

        – Fabio
        Nov 20 '18 at 1:17











      • I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

        – Paolo Go
        Nov 20 '18 at 1:19














      1












      1








      1







      Table class is wrapped within Tables class. So your Tables class should have "internal" collection of itself(Tables)



      public class Company
      {
      public List<Tables> Tables { get; set; }
      }

      // As you said Agri type ignored for now
      public class Tables
      {
      public Table Table { get; set; } // Use collection if Table can be more the one

      [XmlElement("Tables")]
      public Tables InnerTables { get; set; }
      }

      public class Table
      {
      [XmlAttribute]
      public string Id { get; set; }
      }


      Test



      private T DeserialzeFrom<T>(string xml)
      {
      using (var reader = new StringReader(xml))
      {
      return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
      }
      }

      [Test]
      public void ShouldDeserializeCompany()
      {
      var xml = // Build xml string;

      // Expected deserialized object
      var expected = new Company
      {
      Tables = new List<Tables>
      {
      new Tables { Table = new Table { Id = "3" }},
      new Tables { Table = new Table { Id = "4" }}
      }
      };

      var actual = DeserialzeFrom<Company>(xml);

      actual.ShouldBeEquivalentTo(expected); // Test passing
      }





      share|improve this answer













      Table class is wrapped within Tables class. So your Tables class should have "internal" collection of itself(Tables)



      public class Company
      {
      public List<Tables> Tables { get; set; }
      }

      // As you said Agri type ignored for now
      public class Tables
      {
      public Table Table { get; set; } // Use collection if Table can be more the one

      [XmlElement("Tables")]
      public Tables InnerTables { get; set; }
      }

      public class Table
      {
      [XmlAttribute]
      public string Id { get; set; }
      }


      Test



      private T DeserialzeFrom<T>(string xml)
      {
      using (var reader = new StringReader(xml))
      {
      return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
      }
      }

      [Test]
      public void ShouldDeserializeCompany()
      {
      var xml = // Build xml string;

      // Expected deserialized object
      var expected = new Company
      {
      Tables = new List<Tables>
      {
      new Tables { Table = new Table { Id = "3" }},
      new Tables { Table = new Table { Id = "4" }}
      }
      };

      var actual = DeserialzeFrom<Company>(xml);

      actual.ShouldBeEquivalentTo(expected); // Test passing
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 '18 at 0:41









      FabioFabio

      19.3k22044




      19.3k22044













      • Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

        – Paolo Go
        Nov 20 '18 at 0:57











      • @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

        – Fabio
        Nov 20 '18 at 1:17











      • I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

        – Paolo Go
        Nov 20 '18 at 1:19



















      • Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

        – Paolo Go
        Nov 20 '18 at 0:57











      • @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

        – Fabio
        Nov 20 '18 at 1:17











      • I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

        – Paolo Go
        Nov 20 '18 at 1:19

















      Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

      – Paolo Go
      Nov 20 '18 at 0:57





      Thank you for this. I like your answer since it's much cleaner. But how will we add the Agri property to this? I'm trying it too btw :)

      – Paolo Go
      Nov 20 '18 at 0:57













      @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

      – Fabio
      Nov 20 '18 at 1:17





      @PaoloGo, follow same pattern, looks like Agri have partially similar structure to the tables...

      – Fabio
      Nov 20 '18 at 1:17













      I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

      – Paolo Go
      Nov 20 '18 at 1:19





      I posted my own answer. Could you please check if this is what you also have in mind to include Agri?

      – Paolo Go
      Nov 20 '18 at 1:19













      1














      I think this is the class structure you require. With this structure I have serialised the data resulting in a matching XML output. Deserialising your xml above works too.



      public class Company
      {
      public Company()
      {
      this.Tables = new CompanyTables();
      }

      [XmlElement]
      public CompanyTables Tables { get; set; }
      }

      public class CompanyTables
      {
      public CompanyTables()
      {
      this.Agri = new Agri();
      }

      [XmlElement]
      public TablesArr Tables { get; set; }

      [XmlElement]
      public Agri Agri { get; set; }
      }

      public class Agri
      {
      public Agri() { }

      [XmlElementAttribute("Tables")]
      public TablesArr Tables { get; set; }
      }

      public class TablesArr
      {
      public TablesArr() { }

      [XmlElementAttribute("Table")]
      public Table Tables { get; set; }
      }

      public class Table
      {
      public Table() { }

      [XmlAttribute("Id")]
      public int Id { get; set; }
      }


      Serialise test:



      string xml = "<Company><Tables><Agri><Tables><Table Id="1"></Table></Tables><Tables><Table Id="2"></Table></Tables></Agri><Tables><Table Id="3"></Table></Tables><Tables><Table Id="4"></Table></Tables></Tables></Company>";

      XmlSerializer sx = new XmlSerializer(typeof(Company));

      Company company = null;
      using (MemoryStream ms = new MemoryStream())
      {
      ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);

      ms.Flush();

      ms.Seek(0, SeekOrigin.Begin);

      company = (Company)sx.Deserialize(ms);
      }


      Deserialise Test:



      Company company = new TestBed.Company();

      company.Tables.Agri.Tables = new TablesArr[2];
      company.Tables.Agri.Tables[0] = new TablesArr();
      company.Tables.Agri.Tables[0].Tables = new Table[1];
      company.Tables.Agri.Tables[0].Tables[0] = new Table() { Id=1 };
      company.Tables.Agri.Tables[1] = new TablesArr();
      company.Tables.Agri.Tables[1].Tables = new Table[1];
      company.Tables.Agri.Tables[1].Tables[0] = new Table() { Id=2 };

      company.Tables.Tables = new TablesArr[2];
      company.Tables.Tables[0] = new TablesArr();
      company.Tables.Tables[0].Tables = new Table[1];
      company.Tables.Tables[0].Tables[0] = new TestBed.Table() { Id=3 };

      company.Tables.Tables[1] = new TablesArr();
      company.Tables.Tables[1].Tables = new Table[1];
      company.Tables.Tables[1].Tables[0] = new TestBed.Table() { Id=4 };


      XmlSerializer sx = new XmlSerializer(company.GetType());

      using (MemoryStream ms = new MemoryStream())
      {
      sx.Serialize(ms, company);

      ms.Seek(0, SeekOrigin.Begin);

      Console.WriteLine("[{0}]", Encoding.UTF8.GetString(ms.ToArray()));
      }





      share|improve this answer






























        1














        I think this is the class structure you require. With this structure I have serialised the data resulting in a matching XML output. Deserialising your xml above works too.



        public class Company
        {
        public Company()
        {
        this.Tables = new CompanyTables();
        }

        [XmlElement]
        public CompanyTables Tables { get; set; }
        }

        public class CompanyTables
        {
        public CompanyTables()
        {
        this.Agri = new Agri();
        }

        [XmlElement]
        public TablesArr Tables { get; set; }

        [XmlElement]
        public Agri Agri { get; set; }
        }

        public class Agri
        {
        public Agri() { }

        [XmlElementAttribute("Tables")]
        public TablesArr Tables { get; set; }
        }

        public class TablesArr
        {
        public TablesArr() { }

        [XmlElementAttribute("Table")]
        public Table Tables { get; set; }
        }

        public class Table
        {
        public Table() { }

        [XmlAttribute("Id")]
        public int Id { get; set; }
        }


        Serialise test:



        string xml = "<Company><Tables><Agri><Tables><Table Id="1"></Table></Tables><Tables><Table Id="2"></Table></Tables></Agri><Tables><Table Id="3"></Table></Tables><Tables><Table Id="4"></Table></Tables></Tables></Company>";

        XmlSerializer sx = new XmlSerializer(typeof(Company));

        Company company = null;
        using (MemoryStream ms = new MemoryStream())
        {
        ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);

        ms.Flush();

        ms.Seek(0, SeekOrigin.Begin);

        company = (Company)sx.Deserialize(ms);
        }


        Deserialise Test:



        Company company = new TestBed.Company();

        company.Tables.Agri.Tables = new TablesArr[2];
        company.Tables.Agri.Tables[0] = new TablesArr();
        company.Tables.Agri.Tables[0].Tables = new Table[1];
        company.Tables.Agri.Tables[0].Tables[0] = new Table() { Id=1 };
        company.Tables.Agri.Tables[1] = new TablesArr();
        company.Tables.Agri.Tables[1].Tables = new Table[1];
        company.Tables.Agri.Tables[1].Tables[0] = new Table() { Id=2 };

        company.Tables.Tables = new TablesArr[2];
        company.Tables.Tables[0] = new TablesArr();
        company.Tables.Tables[0].Tables = new Table[1];
        company.Tables.Tables[0].Tables[0] = new TestBed.Table() { Id=3 };

        company.Tables.Tables[1] = new TablesArr();
        company.Tables.Tables[1].Tables = new Table[1];
        company.Tables.Tables[1].Tables[0] = new TestBed.Table() { Id=4 };


        XmlSerializer sx = new XmlSerializer(company.GetType());

        using (MemoryStream ms = new MemoryStream())
        {
        sx.Serialize(ms, company);

        ms.Seek(0, SeekOrigin.Begin);

        Console.WriteLine("[{0}]", Encoding.UTF8.GetString(ms.ToArray()));
        }





        share|improve this answer




























          1












          1








          1







          I think this is the class structure you require. With this structure I have serialised the data resulting in a matching XML output. Deserialising your xml above works too.



          public class Company
          {
          public Company()
          {
          this.Tables = new CompanyTables();
          }

          [XmlElement]
          public CompanyTables Tables { get; set; }
          }

          public class CompanyTables
          {
          public CompanyTables()
          {
          this.Agri = new Agri();
          }

          [XmlElement]
          public TablesArr Tables { get; set; }

          [XmlElement]
          public Agri Agri { get; set; }
          }

          public class Agri
          {
          public Agri() { }

          [XmlElementAttribute("Tables")]
          public TablesArr Tables { get; set; }
          }

          public class TablesArr
          {
          public TablesArr() { }

          [XmlElementAttribute("Table")]
          public Table Tables { get; set; }
          }

          public class Table
          {
          public Table() { }

          [XmlAttribute("Id")]
          public int Id { get; set; }
          }


          Serialise test:



          string xml = "<Company><Tables><Agri><Tables><Table Id="1"></Table></Tables><Tables><Table Id="2"></Table></Tables></Agri><Tables><Table Id="3"></Table></Tables><Tables><Table Id="4"></Table></Tables></Tables></Company>";

          XmlSerializer sx = new XmlSerializer(typeof(Company));

          Company company = null;
          using (MemoryStream ms = new MemoryStream())
          {
          ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);

          ms.Flush();

          ms.Seek(0, SeekOrigin.Begin);

          company = (Company)sx.Deserialize(ms);
          }


          Deserialise Test:



          Company company = new TestBed.Company();

          company.Tables.Agri.Tables = new TablesArr[2];
          company.Tables.Agri.Tables[0] = new TablesArr();
          company.Tables.Agri.Tables[0].Tables = new Table[1];
          company.Tables.Agri.Tables[0].Tables[0] = new Table() { Id=1 };
          company.Tables.Agri.Tables[1] = new TablesArr();
          company.Tables.Agri.Tables[1].Tables = new Table[1];
          company.Tables.Agri.Tables[1].Tables[0] = new Table() { Id=2 };

          company.Tables.Tables = new TablesArr[2];
          company.Tables.Tables[0] = new TablesArr();
          company.Tables.Tables[0].Tables = new Table[1];
          company.Tables.Tables[0].Tables[0] = new TestBed.Table() { Id=3 };

          company.Tables.Tables[1] = new TablesArr();
          company.Tables.Tables[1].Tables = new Table[1];
          company.Tables.Tables[1].Tables[0] = new TestBed.Table() { Id=4 };


          XmlSerializer sx = new XmlSerializer(company.GetType());

          using (MemoryStream ms = new MemoryStream())
          {
          sx.Serialize(ms, company);

          ms.Seek(0, SeekOrigin.Begin);

          Console.WriteLine("[{0}]", Encoding.UTF8.GetString(ms.ToArray()));
          }





          share|improve this answer















          I think this is the class structure you require. With this structure I have serialised the data resulting in a matching XML output. Deserialising your xml above works too.



          public class Company
          {
          public Company()
          {
          this.Tables = new CompanyTables();
          }

          [XmlElement]
          public CompanyTables Tables { get; set; }
          }

          public class CompanyTables
          {
          public CompanyTables()
          {
          this.Agri = new Agri();
          }

          [XmlElement]
          public TablesArr Tables { get; set; }

          [XmlElement]
          public Agri Agri { get; set; }
          }

          public class Agri
          {
          public Agri() { }

          [XmlElementAttribute("Tables")]
          public TablesArr Tables { get; set; }
          }

          public class TablesArr
          {
          public TablesArr() { }

          [XmlElementAttribute("Table")]
          public Table Tables { get; set; }
          }

          public class Table
          {
          public Table() { }

          [XmlAttribute("Id")]
          public int Id { get; set; }
          }


          Serialise test:



          string xml = "<Company><Tables><Agri><Tables><Table Id="1"></Table></Tables><Tables><Table Id="2"></Table></Tables></Agri><Tables><Table Id="3"></Table></Tables><Tables><Table Id="4"></Table></Tables></Tables></Company>";

          XmlSerializer sx = new XmlSerializer(typeof(Company));

          Company company = null;
          using (MemoryStream ms = new MemoryStream())
          {
          ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);

          ms.Flush();

          ms.Seek(0, SeekOrigin.Begin);

          company = (Company)sx.Deserialize(ms);
          }


          Deserialise Test:



          Company company = new TestBed.Company();

          company.Tables.Agri.Tables = new TablesArr[2];
          company.Tables.Agri.Tables[0] = new TablesArr();
          company.Tables.Agri.Tables[0].Tables = new Table[1];
          company.Tables.Agri.Tables[0].Tables[0] = new Table() { Id=1 };
          company.Tables.Agri.Tables[1] = new TablesArr();
          company.Tables.Agri.Tables[1].Tables = new Table[1];
          company.Tables.Agri.Tables[1].Tables[0] = new Table() { Id=2 };

          company.Tables.Tables = new TablesArr[2];
          company.Tables.Tables[0] = new TablesArr();
          company.Tables.Tables[0].Tables = new Table[1];
          company.Tables.Tables[0].Tables[0] = new TestBed.Table() { Id=3 };

          company.Tables.Tables[1] = new TablesArr();
          company.Tables.Tables[1].Tables = new Table[1];
          company.Tables.Tables[1].Tables[0] = new TestBed.Table() { Id=4 };


          XmlSerializer sx = new XmlSerializer(company.GetType());

          using (MemoryStream ms = new MemoryStream())
          {
          sx.Serialize(ms, company);

          ms.Seek(0, SeekOrigin.Begin);

          Console.WriteLine("[{0}]", Encoding.UTF8.GetString(ms.ToArray()));
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 0:52

























          answered Nov 20 '18 at 0:42









          AllumearzAllumearz

          1896




          1896























              0














              Here is what I got to include Agri based on @Fabio's answer.



              public class Company
              {
              public CompanyTables Tables { get; set; }
              }

              public class CompanyTables
              {
              [XmlElement]
              public List<Tables> Tables { get; set; }

              public List<Tables> Agri { get; set; }
              }

              public class Tables
              {
              [XmlElement]
              public List<Table> Table { get; set; }
              }

              public class Table
              {
              [XmlAttribute]
              public int Id { get; set; }
              }





              share|improve this answer




























                0














                Here is what I got to include Agri based on @Fabio's answer.



                public class Company
                {
                public CompanyTables Tables { get; set; }
                }

                public class CompanyTables
                {
                [XmlElement]
                public List<Tables> Tables { get; set; }

                public List<Tables> Agri { get; set; }
                }

                public class Tables
                {
                [XmlElement]
                public List<Table> Table { get; set; }
                }

                public class Table
                {
                [XmlAttribute]
                public int Id { get; set; }
                }





                share|improve this answer


























                  0












                  0








                  0







                  Here is what I got to include Agri based on @Fabio's answer.



                  public class Company
                  {
                  public CompanyTables Tables { get; set; }
                  }

                  public class CompanyTables
                  {
                  [XmlElement]
                  public List<Tables> Tables { get; set; }

                  public List<Tables> Agri { get; set; }
                  }

                  public class Tables
                  {
                  [XmlElement]
                  public List<Table> Table { get; set; }
                  }

                  public class Table
                  {
                  [XmlAttribute]
                  public int Id { get; set; }
                  }





                  share|improve this answer













                  Here is what I got to include Agri based on @Fabio's answer.



                  public class Company
                  {
                  public CompanyTables Tables { get; set; }
                  }

                  public class CompanyTables
                  {
                  [XmlElement]
                  public List<Tables> Tables { get; set; }

                  public List<Tables> Agri { get; set; }
                  }

                  public class Tables
                  {
                  [XmlElement]
                  public List<Table> Table { get; set; }
                  }

                  public class Table
                  {
                  [XmlAttribute]
                  public int Id { get; set; }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 1:19









                  Paolo GoPaolo Go

                  1,397620




                  1,397620






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383461%2fhow-do-i-represent-xml-as-classes-when-it-has-multiple-elements-of-same-name%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      Npm cannot find a required file even through it is in the searched directory