c# - How does XML Serialization know where to put the attribute? -


I'm trying to figure out how to do XML serialization

In this way I want to I know that my XML document also looks at the end

   

So far this looks like me

   & Lt; Work id = "second task" /> & Lt; / Course & gt; Using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Xml.Serialization; Namespace Console Application 1 {[XmlRoot ("Course")] Public Category MyWrapper {Public MyWrapper () {TaskList = New List & amp; Work; Work & gt; (); } [XmlElement ("name")] public string name {get; Set; } [XmlElement ("Background Color")] Public String Backgrounder {get; Set; } [XmlElement ("fontColor")] Public String FontColor {get; Set; } [XmlElement ("shareholders")] {get public guide sharing; Set; } [XmlElement ("Work")] Public List & Work; Work & gt; Agenda {Receive; Set; }} Public class function {[XmlAttribute ("id")] Public string id {get; Set; }}} Using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Xml.Serialization; Using System.IO; Namespace console application 1 {class program {static zero main (string [] args) {task work = new function (); Task.Id = "First Job"; Work task2 = new work (); Task2.Id = "second task"; Wrap My Way = New MyWrapper (); Wrap. Background color = "test"; Wrap. Fontclosure = "test2"; Wrap.Name = "name"; Wrap up Sherkingky = Guide. Newguide (); Wrap.TaskList.Add (function); Wrap.TaskList.Add (task2); SerializeToXML (sheet); } Stable Public Zero SerializeToxML (Myvermar List) {XmlSerializer serializer = New XmlSerializer (Type (MyWrapper)); TextWriter textWriter = new streamer (@ "c: \ new folder \ test.xml"); Serializer.Serialize (textWriter, list); TextWriter.Close (); }}  

}

Then my question is with the "id" for the job.

I have another class with it

  [XmlAttribute ("id")] Public string id {get; Set; }  

How did this attribute appear in the task tag?

What if I want to make some other asset?

  [XmlElement ()] Public string TaskName {get; Set;}  

Say that I wanted an attribute with this element, how would I ensure that this task would be done with Taskname along with the task?

  [XmlElement ("work")] Public listing & work; Work & gt; Agenda {Receive; Set; }  

This part of your code asked for it to sort every work object in an element called "work" Serializer work looks at the property of the class and searches for id property that you have marked with the [XmlAttribute ("id")] , the "task" element for the object In the form of a specialty is sorted.

I'm not sure that I understand your second question. You can not add an attribute to the element because its type is a string instead you have to create a new class to wrap the concept of a work name in which the name will be property and the other properties you want to add.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -