项目作者: ITAgnesmeyer

项目描述 :
Easy to use XML Serializer
高级语言: C#
项目地址: git://github.com/ITAgnesmeyer/EasyXMLSerializer.git
创建时间: 2017-12-18T13:18:39Z
项目社区:https://github.com/ITAgnesmeyer/EasyXMLSerializer

开源协议:MIT License

下载


EasyXMLSerializer

Easy to use XML Serializer

You can download this Component aswall by NuGet.

At Package-Manager-Konsole:

  1. PM> install-package EasyXMLSerializer

Usage

Serialize Objects

  1. //Object to be Serialized
  2. public class TestObject
  3. {
  4. public string Name { get; set; }
  5. public string ConnectionString { get; set; }
  6. }

Serialize the Object to a File.

  1. using EasyXMLSerializer;
  2. //...
  3. //Calling Code
  4. // Fill the Object
  5. var testObject = new TestObject
  6. {
  7. Name = "TestName",
  8. ConnectionString = "Server=myServerAddress;..."
  9. };
  10. //Save the Object to "TestObject.xml"
  11. var serializeTool = new SerializeTool("TestObject.xml");
  12. serializeTool.WriteXmlFile(testObject);
  13. //...

Result

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. <Name>TestName</Name>
  4. <ConnectionString>Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;</ConnectionString>
  5. </TestObject>

You can use XMLSerializeation attributes

  1. using System.Xml.Serialization;
  2. [XmlRoot("Configuration")]
  3. public class TestObject
  4. {
  5. [XmlAttribute]
  6. public string Name { get; set; }
  7. public string ConnectionString { get; set; }
  8. }

Result

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="TestName">
  3. <ConnectionString>Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;</ConnectionString>
  4. </Configuration>

Deserialize to Object

Using a File:

  1. var deserializeTool = new SerializeTool("TestObject.xml");
  2. var testObject = deserializeTool.ReadXmlFile<TestObject>();
  3. Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
  4. Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");
  5. //---
  6. //Alternative
  7. var deserializeTool = new SerializeTool();
  8. var testObject = deserializeTool.ReadXmlFile<TestObject>("TestObject.xml");
  9. Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
  10. Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");

Using a XML-String:

  1. var xmlString = "<Configuration Name=\"TestName\"><ConnectionString>Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;</ConnectionString></Configuration>";
  2. var testObject = deserializeTool.ReadXmlFromString<TestObject>(xmlString);
  3. Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
  4. Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");

I hope this helps in many Projects :)

Please feel free to comment or suggest features.