c# - Export a collection of objects (which in turn contains a collection object) to xml -


I have a data object class that stores many properties. Is a folder, and the second one is a string [] of all the files in that array.

What I need to do is write it in xml, as follows:

  & lt; X & gt; & Lt; A & gt; Dir & lt; / A & gt; & Lt; P & gt; File & lt; / H> Then all the files (there is a  string []  data per object array), the directory is nested below the field.  

Is this easily possible? Or is there an external library which can easily do it for me?

Thanks

Use Xml Serializer to work for you?

  using System.Linq; Using System.Collections.Generic; Using System.Xml.Serialization; Using System.IO; Using the system; Nomenclature ns {public class data {public class nested {public string {get; Set; } Public string [] content = {"lazy guy jumped", "the", "moon"}; } Public List & Nested & gt; item; } Static Class Helper {public Static String Toxml & lt; T & gt; (It's T. O.B.J.) where T: class, new () {if (null == obj) empty return; (Var mem = new memorystream ()) using {var ser = new XML serializer (typef (t)); Ser.Serialize (mem, OBJ); Return System. Text. Encoding.Utf 8.GetString (M. Toure ()); }} Public stable t fromXml & lt; T & gt; (This string xml) where T: new () (using var mem = new memorystream (System.Text.Encoding.UTF8.GetBytes (xml)) {var ser = new XmlSerializer (typeof (T)); Return (T) service. Directory (mem);}}} class program {public static zero main (string [] args) {var data = new data {item = new list & lt; data. Nested & gt; {new data. Nested {the = "1"}}} Console.WriteLine (data.ToXml ()); Var clone = Data. ToXml (). FromXml & lt; data & gt; (); console.light line ("Disaryized: {0} ", context references (data, x Console. Light Line ("Similar: {0}", Equal (Data. Toxml (), Clone.Tonxml ());}}}   

What Output

  & lt; xml version = "1.0" encoding = "UTF-8"? & Gt; data xmlns: xsi = "http://www.w3.org/2001/ XMLSchema-instance "xmlns: xsd =" http://www.w3.org/2001/XMLSchema ">  gt; & lt; nested & gt; & lt; stuff & gt; & lt; String & gt; Lazy cow jumped & lt; / string & gt; & lt; string & gt; & lt; / string & gt; & Lt; String & gt; Moon & lt; / String & gt; & Lt; / Luggage & gt; & Lt; & Gt; 1 & lt; / D & gt; & Lt; / Nest & gt; & Lt; / Item & gt; & Lt; / Data & gt; Deserialized: True Identical: True   

There are some cornucas and glax, especially when working with existing XSD, but this is very misleading and documented elsewhere.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -