[Fork] https://stackoverflow.com/questions/55031869/xml-c-sharp-serialization-issues-in-ordering-and-xmlroot-attributes

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;

using NUnit.Framework; //NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/55031869/xml-c-sharp-serialization-issues-in-ordering-and-xmlroot-attributes

	[XmlRoot(ElementName = "export")]
	public partial class SchematicExport
	{
		public const string version = "D";

		[XmlAttribute("version")]
        [System.ComponentModel.Browsable(false), System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		public string Version { get { return version; } set { /* Do nothing */ } }
		
		[XmlElement(Order = 1)]
		public Design design { get; set; }

		[XmlArray(Order = 2)]
		[XmlArrayItem(typeof(Component), ElementName = "comp")]
		public List<Component> components;

		[XmlArray(Order = 3)]
		[XmlArrayItem(typeof(LibPart), ElementName = "libpart")]
		public List<LibPart> libparts;

		[XmlArray(Order = 4)]
		[XmlArrayItem(typeof(Library), ElementName = "library")]
		public List<Library> libraries;

		[XmlArrayItem(typeof(Net), ElementName = "net")]
		public List<Net> nets;
	}

	public partial class SchematicExport
	{
		public class Design
		{
			public string source { get; set; }

			public string date
			{
				get => _date.ToString("dd/MM/yyyy HH:mm:ss");
				set => _date = DateTime.Parse(value);
			}

			private DateTime _date;

			public string tool { get; set; }

			[XmlElement("sheet")]
			public List<Sheet> sheets { get; }

			public class Sheet
			{
				[XmlAttribute]
				public int number { get; set; }

				[XmlAttribute]
				public string name { get; set; }

				[XmlAttribute]
				public string tstamps { get; set; }

				public TitleBlock title_block { get; set; }

				public class TitleBlock
				{
					public string title { get; set; }

					public string company { get; set; }

					public string rev { get; set; }

					public string date
					{
						get => _date.ToString("yyyy-MM-dd");
						set => _date = DateTime.Parse(value);
					}

					private DateTime _date;

					public string source { get; set; }

					[XmlElement("comment")]
					public List<Comment> comments;

					public class Comment
					{
						[XmlAttribute]
						public int number;

						[XmlAttribute]
						public string value;

						public Comment()
						{
						}

						public Comment(int number, string value) : this()
						{
							this.number = number;
							this.value = value;
						}
					}

					public TitleBlock()
					{
						comments = new List<Comment>();
						_date = DateTime.Now;
					}

					public TitleBlock(string title, string company, string rev, string date, string source)
					{
						this.title = title;
						this.company = company;
						this.rev = rev;
						this.date = date;
						this.source = source;
					}
				}

				public Sheet()
				{
					title_block = new TitleBlock();
					tstamps = DateTime.Now.ToFileTime().ToString("X8");
				}

				public Sheet(int number, string name) : this()
				{
					this.number = number;
					this.name = name;
				}
			}

			public Design()
			{
				sheets = new List<Sheet>();
				_date = DateTime.Now;
			}

			public Design(string source, string date, string tool) : this()
			{
				this.source = source;
				this.date = date;
				this.tool = tool;
			}
		}

		public class Component
		{
			[XmlAttribute("ref")]
			public string reference { get; set; }
			public string value { get; set; }
			public string footprint { get; set; }

			[XmlArrayItem(typeof(Field), ElementName = "field")]
			public List<Field> fields;

			public LibSource libsource { get; set; }
			public SheetPath sheetpath { get; set; }
			public string tstamp { get; set; }

			public class LibSource
			{
				[XmlAttribute]
				public string lib { get; set; }
				[XmlAttribute]
				public string part { get; set; }
				[XmlAttribute]
				public string description { get; set; }
			}

			public class SheetPath
			{
				[XmlAttribute]
				public string names { get; set; }
				[XmlAttribute]
				public string tstamps { get; set; }
			}

			public Component()
			{
				fields = new List<Field>();
				libsource = new LibSource();
				sheetpath = new SheetPath();

				tstamp = DateTime.Now.ToFileTime().ToString("X8");
			}

			public Component(string reference, string value, string footprint) : this()
			{
				this.reference = reference;
				this.value = value;
				this.footprint = footprint;
			}
		}

		public class LibPart
		{
			[XmlAttribute]
			public string lib { get; set; }

			[XmlAttribute]
			public string part { get; set; }

			[XmlArrayItem(typeof(Field), ElementName = "field")]
			public List<Field> fields;

			[XmlArrayItem(typeof(string), ElementName = "fp")]
			public List<string> footprints;

			[XmlArrayItem(typeof(Pin), ElementName = "pin")]
			public List<Pin> pins;

			public class Pin
			{
				[XmlAttribute]
				public string num { get; set; }

				[XmlAttribute]
				public string name { get; set; }

				[XmlAttribute]
				public string type { get; set; }

				public Pin()
				{
				}

				public Pin(string num, string name, string type) : this()
				{
					this.num = num;
					this.name = name;
					this.type = type;
				}
			}

			public LibPart()
			{
				fields = new List<Field>();
				footprints = new List<string>();
				pins = new List<Pin>();
			}

			public LibPart(string lib, string part) : this()
			{
				this.lib = lib;
				this.part = part;
			}
		}

		public class Library
		{
			[XmlAttribute]
			public string logical { get; set; }

			public string uri { get; set; }

			public Library() { }

			public Library(string logical, string uri) : this()
			{
				this.logical = logical;
				this.uri = uri;
			}
		}

		public class Net
		{
			[XmlAttribute]
			public string code { get; set; }

			[XmlAttribute]
			public string name { get; set; }

			[XmlElement("node")]
			public List<Node> nodes;

			public class Node
			{
				[XmlAttribute("ref")]
				public string reference { get; set; }

				[XmlAttribute]
				public string pin { get; set; }

				public Node() { }

				public Node(string reference, string pin) : this()
				{
					this.reference = reference;
					this.pin = pin;
				}
			}

			public Net()
			{
				nodes = new List<Node>();
			}

			public Net(string code, string name) : this()
			{
				this.code = code;
				this.name = name;
			}
		}

		public class Field
		{
			[XmlAttribute]
			public string name { get; set; }

			[XmlText]
			public string value { get; set; }

			public Field()
			{
			}

			public Field(string name, string value) : this()
			{
				this.name = name;
				this.value = value;
			}
		}

		public SchematicExport()
		{
			design = new Design();
			components = new List<Component>();
			libparts = new List<LibPart>();
			libraries = new List<Library>();
			nets = new List<Net>();
		}

		public void Serialze(string filename)
		{
			XmlSerializer _xmlSerializer = new XmlSerializer(typeof(SchematicExport));
			// FIXED ensure the file is closed.
			using (var _textWriter = new StreamWriter(filename))
			{
				_xmlSerializer.Serialize(_textWriter, this);
			}
		}

		public static SchematicExport Create(string filename)
		{
			if (!System.IO.File.Exists(filename))
				throw new Exception("File not existing");

			SchematicExport _schematicExport = null;

			XmlSerializer _serializer = new XmlSerializer(typeof(SchematicExport));

			StreamReader _reader = new StreamReader(filename);
			_schematicExport = (SchematicExport)_serializer.Deserialize(_reader);

			return _schematicExport;
		}
	}

    public class TestClass
    {
        public static void Test()
        {
			var filename = "Question55031869.xml";
			
			var root = new SchematicExport();
			
			root.Serialze(filename);
			
			Console.WriteLine(File.ReadAllText(filename));
		}
    }
		
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Uncaught exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;