Reputation: 107
I've got an xml file with such structure:
<panel>
<tr>
<td>
<element>
... smth
</element>
</td>
<td>
<element>
... smth
</element>
</td>
<td>
<element>
... smth
</element>
</td>
</tr>
<tr>
<td>
<element>
... smth
</element>
</td>
<td>
<element>
... smth
</element>
</td>
</tr>
</panel>
This is my object structure:
public class Panel
{
[XmlArray(ElementName="tr")]
[XmlArrayItem(ElementName="td")]
public List<tr> Tr { get; set; }
}
public class tr
{
[XmlArray(ElementName="tr")]
[XmlArrayItem(ElementName="td")]
public List<td> td { get; set; }
}
public class td
{
public Element Element { get; set; }
}
After deserialization I have Panel with 5 tr class objects. Can you help me to correct annotations
Upvotes: 0
Views: 929
Reputation: 3793
Whole code for your serialization:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var lst = new List<Td>();
var p = new Panel();
p.Tr = new List<tr>();
var tr = new tr();
p.Tr.Add(tr);
tr.td = new List<Td>();
tr.td.Add(new Td() { Element = "Val1" });
tr.td.Add(new Td() { Element = "Val2" });
var xmlRel = SerializeObject(p);
FileStream fs = new FileStream("ser.xml", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(xmlRel);
sw.Close();
fs.Close();
}
public static String SerializeObject(Object pObject)
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(Panel));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}
catch (Exception e)
{
System.Console.WriteLine(e);
return null;
}
}
private static String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters);
return (constructedString);
}
}
[XmlRoot(ElementName = "panel")]
public class Panel
{
[XmlElementAttribute("tr")]
public List<tr> Tr { get; set; }
}
public class tr
{
[XmlElementAttribute("td")]
public List<Td> td { get; set; }
}
public class Td
{
[XmlElementAttribute("element")]
public string Element { get; set; }
}
}
Upvotes: 0
Reputation: 23034
Shouldn't it be..
public class Panel
{
[XmlArray(ElementName="panel")] //instead of [XmlArray(ElementName="tr")]
[XmlArrayItem(ElementName="tr")]
public List<tr> Tr { get; set; }
.
.
}
Upvotes: 0
Reputation: 6570
This will work (the contents for the "Element" class are not specified in your example, I just made a string of it):
[XmlRoot(ElementName="panel")]
public class Panel
{
[System.Xml.Serialization.XmlElementAttribute("tr")]
public List<Tr> tr { get; set; }
}
public class Tr
{
[System.Xml.Serialization.XmlElementAttribute("td")]
public List<Td> td { get; set; }
}
public class Td
{
[System.Xml.Serialization.XmlElementAttribute("element")]
public Element Element { get; set; }
}
public class Element
{
[System.Xml.Serialization.XmlText]
public string prop { get; set; }
}
Upvotes: 1