c# - How to change XML root name with XML Serialization? -


I am trying to change the root name when I am serializing XML with C #.

It always takes the name of the class and not the name that I am trying to set it.

  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 Wide Men (String [] ARGs) {MyTest Test = New MyTit (); Test.Test = "gog"; & Lt; MyTest & gt; TestList = new list & lt; MyTest & gt; () {Test}; SerializeToXML (testList); } Stable Public Zero SerializeOoXML (list & lt; MyTest & gt; list) {XmlSerializer serializer = New XmlSerializer (type (list (mytest & gt;)); TextWriter textWriter = new streamer (@ "c: \ new folder \ test.xml"); Serializer.Serialize (textWriter, list); TextWriter.Close (); }}} Using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Xml.Serialization; Name Console Application 1 {{XmlRootAttribute (ElementName = "wild animate", IsNullable = false)) Public class MyTest {[XmlElement ("Test")] Public string test {get; Set; }}}  

Result

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; ArrayOfMyTest xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" & gt; & Lt; MyTest & gt; & Lt; Test & gt; Gog & lt; / Test & gt; & Lt; / MyTest & gt; & Lt; / ArrayOfMyTest & gt;  

It does not change this for WildAnimal. I'm not sure why I got a tutorial.

Edit @ Mark

Thanks, now I see what you are doing, it feels weird that you have to make a cover around it . I have another question that I want to do in this format

  & lt; Root & gt; & Lt; Element & gt; & Lt; Name & gt; & Lt; / Name & gt; & Lt; / Element & gt; & Lt; AnotherElement & gt; & Lt; Products & gt; & Lt; / Products & gt; & Lt; / AnotherElement & gt; & Lt; / Root & gt;  

So, like nested elements, would I have to create a new class for the second part and paste it in the cover?

In your example, MyTest route not is; Are you trying to rename the array? I will write a cover:

  [XmlRoot ("Name Encryption Element")] Public Category MyWrapper {Private list & lt; MyTest & gt; Item = new list & lt; MyTest & gt; (); [XmlElement ("Nameoff Element")] Public List & lt; MyTest & gt; Item {get return item}}}} static zero main () {MyTest test = new MyTest (); Test.Test = "gog"; MyWrapper Cover = New MyWrapper {items = {test}}; SerializeToXML (wrapper); } Stable Public Zero SerializeToxML (Myvermar List) {XmlSerializer serializer = New XmlSerializer (Type (MyWrapper)); (Using textWriter textWriter = new streamer (@ "test.xml")) {serializer.Serialize (textWriter, list); TextWriter.Close (); }}  

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 -