XML serialization, deserialization, and XML serialization

Source: Internet
Author: User

XML serialization, deserialization, and XML serialization

Public class XmlUtility
{
/// <Summary>
/// Deserialization. receives two parameters: xmlFilePath (absolute path of the XML file to be deserialized) and type (Object type of the deserialization XML file)
/// </Summary>
/// <Param name = "xmlFilePath"> </param>
/// <Param name = "type"> </param>
/// <Returns> </returns>
Public static object DeserializeFromXml (string xmlFilePath, Type type)
{
Object result = null;
If (File. Exists (xmlFilePath ))
{
Using (StreamReader reader = new StreamReader (xmlFilePath ))
{
XmlSerializer xs = new XmlSerializer (type );
Result = xs. Deserialize (reader );
}
}
Return result;
}

/// <Summary>
/// Serialization.
/// Receives four parameters: srcObject (object instance), type (object type), and xmlFilePath (absolute path of the serialized xml file ), xmlRootName (root node name in the xml file)
/// When multiple object instances need to be serialized to the same XML file, xmlRootName is the name of the root node that all objects share. If this parameter is not specified ,. net will give a name by default (ArrayOf + object class name)
/// </Summary>
/// <Param name = "srcObject"> </param>
/// <Param name = "type"> </param>
/// <Param name = "xmlFilePath"> </param>
/// <Param name = "xmlRootName"> </param>
Public static void SerializeToXml (object srcObject, Type type, string xmlFilePath, string xmlRootName)
{
If (srcObject! = Null &&! String. IsNullOrEmpty (xmlFilePath ))
{
Type = type! = Null? Type: srcObject. GetType ();

Using (StreamWriter sw = new StreamWriter (xmlFilePath ))
{
XmlSerializer xs = string. IsNullOrEmpty (xmlRootName )?
New XmlSerializer (type ):
New XmlSerializer (type, new XmlRootAttribute (xmlRootName ));
Xs. Serialize (sw, srcObject );
}
}
}

}

The above are the classes written in serialization and deserialization methods.

Call the preceding method:

 

// Deserialization method call

Private SMSConfig GetSmsConfig ()
{

// Obtain the physical path. The following method is the content written separately in the project. You may need to write it yourself.
Var xmlFilePath = WebUtility. GetPhysicalFilePath ("~ /Config/SystemConfig/SMS. config ");

// Call the deserialization method (SMSConfig is a model created based on the serialization node)

Return XmlUtility.DeserializeFromXml(XmlFilePath, typeof (SMSConfig) as SMSConfig;
}

// Call the serialization Method

Public JsonResult SaveSMSConfig (SystemConfigModel model)
{
Var xmlFilePath = WebUtility. GetPhysicalFilePath ("~ /Config/SystemConfig/SMS. config ");

// Call the serialization method (SMSConfig is the model. cs created based on the serialization node) ("SMS" is the root node)
XmlUtility.SerializeToXml(Model. SMSConfig, typeof (SMSConfig), xmlFilePath, "SMS ");
}

Focus on the differences in parameters between the two method calls.

The xml file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<SMS xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<RestIp> https://www.baidu.com/</RestIp>
<RestPort> 83 </RestPort>
<MainAccount> 8a48b5514b0b42909cf120d </MainAccount>
<MainToken> 30bbe361564b98ac4e4cb04f795 </MainToken>
<AppId> 8a48b5514b04b242ea5201228 </AppId>
<RegistTemplateId> 244 </RegistTemplateId>
<RegistSuccessTemplateId> 249 </RegistSuccessTemplateId>
<ResetPasswordTemplateId> 250 </ResetPasswordTemplateId>
<NotifyTemplateId> 249 </NotifyTemplateId>
</SMS>

Create model. cs Based on the xml file:

[XmlRoot (ElementName = "SMS")]
Public class SMSConfig
{
[Required (ErrorMessage = "Enter RestIp")]
[XmlElement (ElementName = "RestIp")]
Public string RestIp {get; set ;}

[Required (ErrorMessage = "Enter RestPort")]

[XmlElement (ElementName = "RestPort")]
Public string RestPort {get; set ;}

[Required (ErrorMessage = "Enter MainAccount")]
[XmlElement (ElementName = "MainAccount")]
Public string MainAccount {get; set ;}

[Required (ErrorMessage = "Enter MainToken")]
[XmlElement (ElementName = "MainToken")]
Public string MainToken {get; set ;}

[Required (ErrorMessage = "Enter AppId")]
[XmlElement (ElementName = "AppId")]
Public string AppId {get; set ;}

[Required (ErrorMessage = "Enter RegistTemplateId")]
[XmlElement (ElementName = "RegistTemplateId")]
Public string RegistTemplateId {get; set ;}

[Required (ErrorMessage = "Enter RegistSuccessTemplateId")]
[XmlElement (ElementName = "RegistSuccessTemplateId")]
Public string RegistSuccessTemplateId {get; set ;}

[Required (ErrorMessage = "Enter ResetPasswordTemplateId")]
[XmlElement (ElementName = "ResetPasswordTemplateId")]
Public string ResetPasswordTemplateId {get; set ;}

[Required (ErrorMessage = "Enter policytemplateid")]
[XmlElement (ElementName = "yytemplateid")]
Public string policytemplateid {get; set ;}

}

 

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.