in Education by
I am currently using a LINQ query to read an XML file e.g. some_text some_more_text into a list of custom objects containing custom HistoryString properties. HistoryString contains 2 strings, a currentValue and a previousValue. This all works great except when using XmlSerializer to write the custom objects back to an XML file, the output fairly obviously contains additional tags i.e. some_text some_text some_more_text some_more_text Q: What would be the neatest and/or most efficient way of reading and writing XML in the same format, based on this fundamental difference? Some initial ideas: 1) Mark the previousValue property with [System.Xml.Serialization.XmlIgnore] then sweep through the XML string that is to be written removing all traces of and 2) Open the existing file and manually make any updates/deletes/additions - this is surely more long winded. 3) Any way of having a HistoryString automatically resolve to its currentValue rather than serialize each of its properties, similar to how ToString() works? I have done some research into this, including the useful MSDN articles here and here but I can't see any other attributes that would solve this problem, I am still unsure whether this is possible. Any ideas? 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
Here is another idea. If you define your class like so: [Serializable] public class MyObject { [XmlElement(ElementName = "MyElement")] public string CurrentValueElement { get { return Element.CurrentValue; } set { Element = new MyElement { CurrentValue = value, PreviousValue = value }; } } [XmlElement(ElementName = "MyOtherElement")] public string CurrentValueOtherElement { get { return OtherElement.CurrentValue; } set {} } [XmlIgnore] public MyElement Element { get; set; } [XmlIgnore] public MyElement OtherElement { get; set; } } Then, when the object is serialized, the output XML will look exactly like your example. Also, if you extend the CurrentValueElement/CurrentValueOtherElement setter like this: [XmlElement(ElementName = "MyElement")] public string CurrentValueElement { get { return Element.CurrentValue; } set { Element = new MyElement { CurrentValue = value, PreviousValue = value }; } } Then you'll be able to use the XmlSerializer to deserialize your objects directly without needing to resorting to LINQ.

Related questions

0 votes
    I am developing a c# application, in which the server gets requests from many clients at a time. Each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    We have a requirement for a dexterity content type to have exclude from navigation behaviour but for the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    This method is in a Scores class, that is set the class variable scores, but the scores is combination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I have an XML with Text elements that can include tagged text and depending on the elements attribute value ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    An inverse network may be obtained by converting each resistance element R into a corresponding resistive element of ... EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Finding the location of the element with a given value is: (a) Traversal (b) Search (c) Sort (d ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What are the default values for the packaging element? If there is no packaging element defined? What is the default value for that?...
asked May 4, 2021 in Technology by Editorial Staff
0 votes
    I want put perfect percentage on each chart column as shown in fig in C# Windows Forms. How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I want put perfect percentage on each chart column as shown in fig in C# Windows Forms. How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have a Label with defined width and height, and I want text added to it to be clipped if it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I have created floating label in input field.In chrome view is working fine, but not properly viewed in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I am exploring Float Label pattern for web page using HTML and CSS. The code was referred from http: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
...