Recently, I like to use XML (C #'s Linq to XML ). After writing a class, I always want to save it as an XML file. Generally, I write a ToXElement function and then write it ......
I am so bored today that I want to write a common tool class so that I can easily save a class as XML.
Look at the written results:
The static class Test is used for testing. Test results:
1 /************************************** **********************************
2 * Format an object as XML (element), that is:
3*1. The XML element name is of the object type.
4*2. convert each public Property in the object to a String)
5 * Save to attributes of XML elements
6 * Paste an XML file into an object, that is, the inverse process of the previous process.
7 *
8 * saving object information to XML (and then saving it to a file) is obviously highly readable.
9 *************************************** *********************************/
10 using System;
11 using System. Collections. Generic;
12 using System. Linq;
13 using System. Text;
14 using System. Xml. Linq;
15 namespace DotNetEx. FormatAsXml
16 {
17 /// <summary>
18 // The identifier can be formatted as XML and can be used in the class and structure. It cannot be inherited, and multiple tags are not supported.
19 /// </summary>
20 [AttributeUsage (AttributeTargets. Class | AttributeTargets. Struct, Inherited = false,
21 AllowMultiple = false)]
22 public sealed class CanFormatToXmlAttribute: Attribute
23 {
24 public CanFormatToXmlAttribute (){}
25}
26 /// <summary>
27 // identify this attribute without formatting
28 /// </summary>
29 [AttributeUsage (AttributeTargets. Property, Inherited = false,
30 AllowMultiple = false)]
31 public sealed class DonotFormatToXmlAttribute: Attribute