How do I represent XML as classes when it has multiple elements of same name
up vote
2
down vote
favorite
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.
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
add a comment |
up vote
2
down vote
favorite
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.
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
but doesn't seem to work. - can you elaborate what not working, how you check it is not working...
– Fabio
Nov 19 at 22:26
@Fabio please see updated question.
– Paolo Go
Nov 20 at 0:16
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
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
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.
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
c# xml xmlserializer
edited Nov 20 at 0:16
asked Nov 19 at 22:18
Paolo Go
1,335619
1,335619
but doesn't seem to work. - can you elaborate what not working, how you check it is not working...
– Fabio
Nov 19 at 22:26
@Fabio please see updated question.
– Paolo Go
Nov 20 at 0:16
add a comment |
but doesn't seem to work. - can you elaborate what not working, how you check it is not working...
– Fabio
Nov 19 at 22:26
@Fabio please see updated question.
– Paolo Go
Nov 20 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 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 at 22:26
@Fabio please see updated question.
– Paolo Go
Nov 20 at 0:16
@Fabio please see updated question.
– Paolo Go
Nov 20 at 0:16
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
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
}
Thank you for this. I like your answer since it's much cleaner. But how will we add theAgri
property to this? I'm trying it too btw :)
– Paolo Go
Nov 20 at 0:57
@PaoloGo, follow same pattern, looks likeAgri
have partially similar structure to the tables...
– Fabio
Nov 20 at 1:17
I posted my own answer. Could you please check if this is what you also have in mind to includeAgri
?
– Paolo Go
Nov 20 at 1:19
add a comment |
up vote
1
down vote
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()));
}
add a comment |
up vote
0
down vote
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; }
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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
}
Thank you for this. I like your answer since it's much cleaner. But how will we add theAgri
property to this? I'm trying it too btw :)
– Paolo Go
Nov 20 at 0:57
@PaoloGo, follow same pattern, looks likeAgri
have partially similar structure to the tables...
– Fabio
Nov 20 at 1:17
I posted my own answer. Could you please check if this is what you also have in mind to includeAgri
?
– Paolo Go
Nov 20 at 1:19
add a comment |
up vote
1
down vote
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
}
Thank you for this. I like your answer since it's much cleaner. But how will we add theAgri
property to this? I'm trying it too btw :)
– Paolo Go
Nov 20 at 0:57
@PaoloGo, follow same pattern, looks likeAgri
have partially similar structure to the tables...
– Fabio
Nov 20 at 1:17
I posted my own answer. Could you please check if this is what you also have in mind to includeAgri
?
– Paolo Go
Nov 20 at 1:19
add a comment |
up vote
1
down vote
up vote
1
down vote
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
}
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
}
answered Nov 20 at 0:41
Fabio
18.9k22044
18.9k22044
Thank you for this. I like your answer since it's much cleaner. But how will we add theAgri
property to this? I'm trying it too btw :)
– Paolo Go
Nov 20 at 0:57
@PaoloGo, follow same pattern, looks likeAgri
have partially similar structure to the tables...
– Fabio
Nov 20 at 1:17
I posted my own answer. Could you please check if this is what you also have in mind to includeAgri
?
– Paolo Go
Nov 20 at 1:19
add a comment |
Thank you for this. I like your answer since it's much cleaner. But how will we add theAgri
property to this? I'm trying it too btw :)
– Paolo Go
Nov 20 at 0:57
@PaoloGo, follow same pattern, looks likeAgri
have partially similar structure to the tables...
– Fabio
Nov 20 at 1:17
I posted my own answer. Could you please check if this is what you also have in mind to includeAgri
?
– Paolo Go
Nov 20 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 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 at 0:57
@PaoloGo, follow same pattern, looks like
Agri
have partially similar structure to the tables...– Fabio
Nov 20 at 1:17
@PaoloGo, follow same pattern, looks like
Agri
have partially similar structure to the tables...– Fabio
Nov 20 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 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 at 1:19
add a comment |
up vote
1
down vote
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()));
}
add a comment |
up vote
1
down vote
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()));
}
add a comment |
up vote
1
down vote
up vote
1
down vote
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()));
}
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()));
}
edited Nov 20 at 0:52
answered Nov 20 at 0:42
Allumearz
1896
1896
add a comment |
add a comment |
up vote
0
down vote
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; }
}
add a comment |
up vote
0
down vote
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; }
}
add a comment |
up vote
0
down vote
up vote
0
down vote
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; }
}
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; }
}
answered Nov 20 at 1:19
Paolo Go
1,335619
1,335619
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
but doesn't seem to work. - can you elaborate what not working, how you check it is not working...
– Fabio
Nov 19 at 22:26
@Fabio please see updated question.
– Paolo Go
Nov 20 at 0:16