0

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

1
  • I suppose you meant to put a closing "</panel>" tag in the XML section? Commented Jan 27, 2011 at 11:02

3 Answers 3

1

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it works perfect. Thank you! I've changed XmlElementAttribute to XmlElement
0

Shouldn't it be..

public class Panel
{
    [XmlArray(ElementName="panel")] //instead of [XmlArray(ElementName="tr")]
    [XmlArrayItem(ElementName="tr")]
    public List<tr> Tr { get; set; }
.
.
}

Comments

0

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; }
}

}

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.