in Education by
Since XmlSerializer can not serialize any other properties when the class is inherited from List <>, I try to solve them with the DataContractSerializer. This should work, as described here: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes But I get the same results. If the object is inherited from List <> the TestValue property is not serialized. using System.Runtime.Serialization; [Serializable] public class XSerBase { [DataMember] public XSerTest XSerTest { get; set; } = new XSerTest(); } [Serializable] public class XSerTest : List { [DataMember] public string TestValue { get; set; } } {// my serialize / deserialize example XSerBase objectSource = new XSerBase(); objectSource.XSerTest.TestValue = "QWERT"; MemoryStream mem = new MemoryStream(); DataContractSerializer dcsSource = new DataContractSerializer(typeof(XSerBase)); dcsSource.WriteObject(mem, objectSource); mem.Position = 0; XSerBase objectDestination = null; DataContractSerializer dcsDestination = new DataContractSerializer(typeof(XSerBase)); objectDestination = (dcsDestination.ReadObject(mem) as XSerBase); // objectDestination.XSerTest.TestValue is null // objectDestination.XSerTest.TestValue is "QWERT", when XSerTest is not inherited from List } Am I missing an attribute? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I tried to get an inherited class List to work and was not successful. This is the best I could do using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication106 { class Program { const string FILENAME = @"c:\temp\test.xml"; static void Main(string[] args) { XSerBase test = new XSerBase() { XSerTest = new List() { new XSerTest() { TestValue = "123"}, new XSerTest() { TestValue = "456"} } }; XmlSerializer serializer = new XmlSerializer(typeof(XSerBase)); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(FILENAME,settings); serializer.Serialize(writer, test); writer.Flush(); writer.Close(); } } public class XSerBase { [XmlElement("XSerTest")] public List XSerTest { get; set; } } public class XSerTest { public string TestValue { get; set; } } }

Related questions

0 votes
    Which of the following does JSON.stringify not serialize? (a) Undefined values (b) Functions within an ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    All classes in Java are inherited from which class? (a) java.lang.class (b) java.class.inherited ( ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    How do you prevent a class from being inherited?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    I'm not sure how exactly to phrase my question. So, I have an interface reference and I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    When you call serialize() on an R object, the output will be ____ coded in hexadecimal format. (a) raw ... Operations of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How many properties does a prototype object have? (a) 6 (b) 7 (c) 8 (d) 9 This question ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    I have a field call query_data defined as text in my MySQL database. In my model I defined this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
0 votes
    Explain what is DataContractSerializer?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    Static members are not inherited to subclass. (a) True (b) False This question was addressed to me ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
...