I have looked at several other questions for this same issue but have not been able to resolve my problem. I have classes, code, and XML as follows. However, after the Deserialize call my type variable contains a TestList array which contains a TestElement but the TestElement is null. Appreciate any help. Thanks.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace TestXMLSerialization
{
class Program
{
static void Main(string[] args) {
XmlSerializer ser = new XmlSerializer(typeof(TestRootElement));
string xmlString = "<?xml version=\"1.0\" ?><TestRootElement><TestList><TestItem><TestElement>Test Data</TestElement></TestItem></TestList></TestRootElement>";
TestRootElement type = (TestRootElement)ser.Deserialize(new StringReader(xmlString));
Console.WriteLine(type.TestList[0].TestElement);
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("TestRootElement")]
public class TestRootElement
{
[System.Xml.Serialization.XmlElement("TestList")]
public List<TestItem> TestList { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlType("TestItem")]
public class TestItem
{
[System.Xml.Serialization.XmlElement("TestElement")]
public string TestElement { get; set; }
}
}
XmlRootattributes - a document has one root, in this caseTestRootElement.