C # tool class for XML format Storage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.