Let the class determine how to serialize XML.

Source: Internet
Author: User
We do not need to describe the classes we define for XML serialization. How can we customize this process and let the classes manage this serialization process by themselves? I found it for a long time and failed to get any source code prompt.

XML serialization is divided into two types: XML serialization and soap serialization.

I am referring to the serialized class for XML serialization. No additional code is required.

// Serialization
Textwriter writer = NULL;
Try
{
Xmlserializer SER = new xmlserializer (typeof (nhfroggenerator. memberproperty ));
Writer = new streamwriter ("D: \ temp \ myfilename. xml ");
Ser. serialize (writer, Pro );
}
Catch (exception ee)
{
MessageBox. Show (EE. tostring ());
}
Finally
{
If (writer! = NULL)
Writer. Close ();
}

For soap serialization, the attribute of the class must be added as serializable. In fact, the serialization process is similar to that of XML.

// Serialization
Stream writer = NULL;
Try
{
Soapformatter formatter = new soapformatter ();
Writer = file. openwrite ("D: \ temp \ myfilename. xml ");
Formatter. serialize (writer, Pro );
}
Catch (exception ee)
{
MessageBox. Show (EE. tostring ());
}
Finally
{
If (writer! = NULL)
Writer. Close ();
}

The namespaces are different.
XML is system. xml. serialization.
Soap is system. runtime. serialization. formatters. Soap

I thought iserializable could help me customize the XML serialization process, put a breakpoint, and found that it was not executed, so I used the soap test. Haha, iserializable is used by soap. I found another interface ixmlserializable.
These three methods are unknown.

Public void writexml (system. xml. xmlwriter writer)
{
// Todo: add the memberproperty. writexml implementation
}

Public System. xml. schema. XMLSCHEMA getschema ()
{
// Todo: Add memberproperty. getschema implementation
Return NULL;
}

Public void readxml (system. xml. xmlreader reader)
{
// Todo: add the memberproperty. readxml implementation.
}

The document does not provide any help "This Member supports the. NET Framework Structure, so it is not applicable to use directly from the code .". Writexml thinks that writestart is required, and then writeend, which can cause an error. The error message "XML document format error" is displayed. I tried it several times.
Writer. writeelementstring ("columnname", this. columnname );
OK.

Readxml makes me a lot of detours. Reader corresponds to the entire XML document, while xmlreader is a read-only object. Multiple or fewer reads of a class will affect subsequent deserialization. The following is my statement. Basic test passed!

Public void writexml (system. xml. xmlwriter writer)
{
Writer. writeelementstring ("columnname", this. columnname );
Writer. writeelementstring ("columntype", String. Format ("{0}, nhfroggenerator. typelist", this. columntype ));
Writer. writeelementstring ("length", this. length. tostring ());
If (this. isprimarykey)
Writer. writeelementstring ("primarykey", "true ");
}

Public System. xml. schema. XMLSCHEMA getschema ()
{
// Todo: Add memberproperty. getschema implementation
Return NULL;
}

Public void readxml (system. xml. xmlreader reader)
{
// Todo: add the memberproperty. readxml implementation.
Bool blrun = true;
While (blrun)
{
Switch (reader. localname)
{
Case "columntype ":
String strtype = reader. readelementstring ();
Break;
Case "columnname ":
This. columnname = reader. readelementstring ();
Break;
Case "length ":
String ilen = reader. readelementstring ();
Break;
Case "primarykey ":
String strkey = reader. readelementstring ();
Break;
Default:
Blrun = reader. Read ();
Break;
}
If (reader. nodetype = xmlnodetype. endelement) // it has reached the end of the node.
{
Reader. readendelement (); // readendelement is required once !!
Blrun = false;
}
}
}

Well, all technical difficulties have been solved one by one. You can open your hand... haha, happy...

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.