{"id":195,"date":"2025-08-19T02:13:19","date_gmt":"2025-08-19T02:13:19","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=195"},"modified":"2025-08-19T02:37:28","modified_gmt":"2025-08-19T02:37:28","slug":"%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87%e0%b9%84%e0%b8%9f%e0%b8%a5%e0%b9%8c-xml-%e0%b8%88%e0%b8%b2%e0%b8%81-object-%e0%b9%83%e0%b8%99-c","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2025\/08\/19\/%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87%e0%b9%84%e0%b8%9f%e0%b8%a5%e0%b9%8c-xml-%e0%b8%88%e0%b8%b2%e0%b8%81-object-%e0%b9%83%e0%b8%99-c\/","title":{"rendered":"\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e44\u0e1f\u0e25\u0e4c XML \u0e08\u0e32\u0e01 Object \u0e43\u0e19 C#"},"content":{"rendered":"\n<p>\u0e40\u0e23\u0e34\u0e48\u0e21\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23\u0e40\u0e15\u0e23\u0e35\u0e22\u0e21 Class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    [Serializable]\n    [XmlRoot(\"PeopleData\")]\n    public class People\n    {\n        [XmlArray(\"UserList\")]\n        [XmlArrayItem(\"User\")]\n        public List&lt;Person> PersonList { get; set; }\n\n        public People()\n        {\n            PersonList = new List&lt;Person>();\n        }\n    }<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    [Serializable]\n    public class Person\n    {\n        [XmlElement(\"FullName\")]\n        public string Name { get; set; }\n\n        [XmlElement(\"CurrentAge\")]\n        public int Age { get; set; }\n\n        [XmlAttribute(\"ID\")]\n        public int Id { get; set; }\n\n        [XmlArray(\"Hobbies\")]\n        [XmlArrayItem(\"Hobby\")]\n        public List&lt;string> Hobbies { get; set; }\n\n        public Person() \n        {\n            Hobbies = new List&lt;string>();\n        }\n    }<\/pre>\n\n\n\n<p>\u0e2a\u0e23\u0e49\u0e32\u0e07 Object &#8211; \u0e43\u0e2a\u0e48\u0e04\u0e48\u0e32\u0e25\u0e07\u0e43\u0e19 Object<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    public static class XmlHelper\n    {\n        public static void ExportToXml&lt;T>(T data, string filePath)\n        {\n            try\n            {\n                XmlSerializer serializer = new XmlSerializer(typeof(T));\n                using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8))\n                {\n                    serializer.Serialize(writer, data);\n                }\n                Console.WriteLine($\"Export \u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e44\u0e1b\u0e17\u0e35\u0e48 '{filePath}' \u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08\");\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine($\"\u0e40\u0e01\u0e34\u0e14\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07 Export: {ex.Message}\");\n            }\n        }<\/pre>\n\n\n\n<p>Export \u0e40\u0e1b\u0e47\u0e19 XML<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    class Program\n    {\n        static void Main(string[] args)\n        {\n            string xmlFilePath = \"data.xml\";\n            \n            var peopleData = new People();\n            peopleData.PersonList.Add(new Person { Id = 1, Name = \"\u0e2a\u0e21\u0e0a\u0e32\u0e22 \u0e43\u0e08\u0e14\u0e35\", Age = 30, Hobbies = new List&lt;string> { \"\u0e2d\u0e48\u0e32\u0e19\u0e2b\u0e19\u0e31\u0e07\u0e2a\u0e37\u0e2d\" } });\n            peopleData.PersonList.Add(new Person { Id = 2, Name = \"\u0e2a\u0e21\u0e2b\u0e0d\u0e34\u0e07 \u0e08\u0e23\u0e34\u0e07\u0e43\u0e08\", Age = 25, Hobbies = new List&lt;string> { \"\u0e1f\u0e31\u0e07\u0e40\u0e1e\u0e25\u0e07\", \"\u0e27\u0e34\u0e48\u0e07\" } });\n            XmlHelper.ExportToXml(peopleData, xmlFilePath);\n            Console.WriteLine(\"\\n-----------------------------------\\n\");\n        }\n    }<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"235\" src=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-42.png\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-42.png 634w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/08\/image-42-300x111.png 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\n&lt;PeopleData xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\">\n  &lt;UserList>\n    &lt;User ID=\"1\">\n      &lt;FullName>\u0e2a\u0e21\u0e0a\u0e32\u0e22 \u0e43\u0e08\u0e14\u0e35&lt;\/FullName>\n      &lt;CurrentAge>30&lt;\/CurrentAge>\n      &lt;Hobbies>\n        &lt;Hobby>\u0e2d\u0e48\u0e32\u0e19\u0e2b\u0e19\u0e31\u0e07\u0e2a\u0e37\u0e2d&lt;\/Hobby>\n      &lt;\/Hobbies>\n    &lt;\/User>\n    &lt;User ID=\"2\">\n      &lt;FullName>\u0e2a\u0e21\u0e2b\u0e0d\u0e34\u0e07 \u0e08\u0e23\u0e34\u0e07\u0e43\u0e08&lt;\/FullName>\n      &lt;CurrentAge>25&lt;\/CurrentAge>\n      &lt;Hobbies>\n        &lt;Hobby>\u0e1f\u0e31\u0e07\u0e40\u0e1e\u0e25\u0e07&lt;\/Hobby>\n        &lt;Hobby>\u0e27\u0e34\u0e48\u0e07&lt;\/Hobby>\n      &lt;\/Hobbies>\n    &lt;\/User>\n  &lt;\/UserList>\n&lt;\/PeopleData><\/pre>\n\n\n\n<p>\u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e2d\u0e48\u0e32\u0e19\u0e01\u0e25\u0e31\u0e1a\u0e21\u0e32\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\/ &lt;summary>\n\/\/\/ Import \u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e08\u0e32\u0e01\u0e44\u0e1f\u0e25\u0e4c XML \u0e1e\u0e23\u0e49\u0e2d\u0e21 Validate \u0e01\u0e31\u0e1a XSD (\u0e16\u0e49\u0e32\u0e21\u0e35)\n\/\/\/ &lt;\/summary>\n\/\/\/ &lt;param name=\"xsdPath\">Path \u0e02\u0e2d\u0e07\u0e44\u0e1f\u0e25\u0e4c XSD (\u0e16\u0e49\u0e32\u0e40\u0e1b\u0e47\u0e19 null \u0e08\u0e30\u0e44\u0e21\u0e48\u0e17\u0e33\u0e01\u0e32\u0e23 Validate)&lt;\/param>\npublic static T ImportFromXml&lt;T>(string filePath, string xsdPath)\n{\n    if (!File.Exists(filePath))\n    {\n        throw new FileNotFoundException(\"\u0e44\u0e21\u0e48\u0e1e\u0e1a\u0e44\u0e1f\u0e25\u0e4c XML \u0e17\u0e35\u0e48\u0e23\u0e30\u0e1a\u0e38\", filePath);\n    }\n\n    \/\/ \u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e40\u0e01\u0e47\u0e1a\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23 Validate\n    var validationErrors = new List&lt;string>();\n\n    try\n    {\n        \/\/ --- \u0e2a\u0e48\u0e27\u0e19\u0e02\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e15\u0e31\u0e49\u0e07\u0e04\u0e48\u0e32 Validation ---\n        var settings = new XmlReaderSettings();\n        if (!string.IsNullOrEmpty(xsdPath))\n        {\n            if (!File.Exists(xsdPath))\n            {\n                throw new FileNotFoundException(\"\u0e44\u0e21\u0e48\u0e1e\u0e1a\u0e44\u0e1f\u0e25\u0e4c XSD \u0e17\u0e35\u0e48\u0e23\u0e30\u0e1a\u0e38\", xsdPath);\n            }\n\n            \/\/ \u0e40\u0e1e\u0e34\u0e48\u0e21 Schema \u0e40\u0e02\u0e49\u0e32\u0e44\u0e1b\u0e43\u0e19\u0e01\u0e32\u0e23\u0e15\u0e31\u0e49\u0e07\u0e04\u0e48\u0e32\n            settings.Schemas.Add(null, xsdPath); \/\/ targetNamespace = null\n            settings.ValidationType = ValidationType.Schema;\n\n            \/\/ \u0e2a\u0e23\u0e49\u0e32\u0e07 Event Handler \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e14\u0e31\u0e01\u0e08\u0e31\u0e1a Error \u0e15\u0e2d\u0e19 Validate\n            settings.ValidationEventHandler += (sender, args) =>\n            {\n                if (args.Severity == XmlSeverityType.Error)\n                {\n                    validationErrors.Add($\"\u0e1a\u0e23\u0e23\u0e17\u0e31\u0e14\u0e17\u0e35\u0e48 {args.Exception.LineNumber}, \u0e15\u0e33\u0e41\u0e2b\u0e19\u0e48\u0e07\u0e17\u0e35\u0e48 {args.Exception.LinePosition}: {args.Message}\");\n                }\n            };\n        }\n\n        \/\/ --- \u0e17\u0e33\u0e01\u0e32\u0e23\u0e2d\u0e48\u0e32\u0e19\u0e41\u0e25\u0e30 Deserialize ---\n        using (XmlReader reader = XmlReader.Create(filePath, settings))\n        {\n            XmlSerializer serializer = new XmlSerializer(typeof(T));\n            T result = (T)serializer.Deserialize(reader);\n\n            \/\/ \u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01 Deserialize \u0e40\u0e2a\u0e23\u0e47\u0e08, \u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a\u0e27\u0e48\u0e32\u0e21\u0e35 Error \u0e2b\u0e23\u0e37\u0e2d\u0e44\u0e21\u0e48\n            if (validationErrors.Count > 0)\n            {\n                throw new XmlSchemaValidationException(\"\u0e44\u0e1f\u0e25\u0e4c XML \u0e44\u0e21\u0e48\u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07\u0e15\u0e32\u0e21 Schema:\\n\" + string.Join(\"\\n\", validationErrors));\n            }\n\n            return result;\n        }\n    }\n    catch (Exception ex)\n    {\n        Console.WriteLine($\"\u0e40\u0e01\u0e34\u0e14\u0e02\u0e49\u0e2d\u0e1c\u0e34\u0e14\u0e1e\u0e25\u0e32\u0e14\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07 Import: {ex.Message}\");\n        return default(T);\n    }\n}<\/pre>\n\n\n\n<p>\u0e43\u0e19\u0e01\u0e23\u0e13\u0e35\u0e17\u0e35\u0e48\u0e21\u0e35 XSD \u0e14\u0e49\u0e27\u0e22 \u0e40\u0e23\u0e32\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e2a\u0e48\u0e07\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19 validate \u0e44\u0e14\u0e49\u0e14\u0e49\u0e27\u0e22<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Console.WriteLine($\"--- \u0e17\u0e14\u0e2a\u0e2d\u0e1a Import '{xmlFilePath}' (\u0e44\u0e1f\u0e25\u0e4c\u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07) \u0e1e\u0e23\u0e49\u0e2d\u0e21 Validate \u0e01\u0e31\u0e1a XSD ---\");\nPeople importedData = XmlHelper.ImportFromXml&lt;People>(xmlFilePath, xsdFilePath);\nif (importedData != null)\n{\n    Console.WriteLine(\"Import \u0e41\u0e25\u0e30 Validate \u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08! \u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07\u0e15\u0e32\u0e21 Schema\");\n    foreach (var person in importedData.PersonList)\n    {\n        Console.WriteLine($\"  - ID: {person.Id}, \u0e0a\u0e37\u0e48\u0e2d: {person.Name}, \u0e2d\u0e32\u0e22\u0e38: {person.Age}\");\n    }\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0e40\u0e23\u0e34\u0e48\u0e21\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23\u0e40\u0e15\u0e23\u0e35\u0e22\u0e21 Class \u0e2a\u0e23\u0e49\u0e32\u0e07 Object &#8211; \u0e43\u0e2a\u0e48\u0e04\u0e48\u0e32\u0e25\u0e07\u0e43\u0e19 Object Export \u0e40\u0e1b\u0e47\u0e19 XML \u0e16\u0e49\u0e32\u0e40\u0e23\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e2d\u0e48\u0e32\u0e19\u0e01\u0e25\u0e31\u0e1a\u0e21\u0e32\u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19 \u0e43\u0e19\u0e01\u0e23\u0e13\u0e35\u0e17\u0e35\u0e48\u0e21\u0e35 XSD \u0e14\u0e49\u0e27\u0e22 \u0e40\u0e23\u0e32\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e2a\u0e48\u0e07\u0e43\u0e2b\u0e49\u0e21\u0e31\u0e19&hellip;<\/p>\n","protected":false},"author":1,"featured_media":199,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,2],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-programming"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":3,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/195\/revisions\/207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media\/199"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}