Code Monkey home page Code Monkey logo

easyxmlserializer's Introduction

EasyXMLSerializer

Easy to use XML Serializer

You can download this Component aswall by NuGet.

At Package-Manager-Konsole:

PM> install-package EasyXMLSerializer

Usage

Serialize Objects

//Object to be Serialized
public class TestObject
{
   public string Name { get; set; }
   public string ConnectionString { get; set; }
}

Serialize the Object to a File.

using EasyXMLSerializer;
//...

//Calling Code

// Fill the Object
var testObject = new TestObject
{
	Name = "TestName",
	ConnectionString = "Server=myServerAddress;..."
};

//Save the Object to "TestObject.xml"
var serializeTool = new SerializeTool("TestObject.xml");
serializeTool.WriteXmlFile(testObject);

//...

Result

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

You can use XMLSerializeation attributes

using System.Xml.Serialization;

[XmlRoot("Configuration")]
public class TestObject
{
	[XmlAttribute]
	public string Name { get; set; }
	public string ConnectionString { get; set; }
}

Result

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

Deserialize to Object

Using a File:

var deserializeTool = new SerializeTool("TestObject.xml");
var testObject = deserializeTool.ReadXmlFile<TestObject>();

Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");

//---

//Alternative
var deserializeTool = new SerializeTool();
var testObject = deserializeTool.ReadXmlFile<TestObject>("TestObject.xml");
Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");

Using a XML-String:

var xmlString = "<Configuration Name=\"TestName\"><ConnectionString>Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;</ConnectionString></Configuration>";
var testObject = deserializeTool.ReadXmlFromString<TestObject>(xmlString);

Console.WriteLine($@"TestObject.Name=>{testObject.Name}");
Console.WriteLine($@"TestObject.ConnectionString=>{testObject.ConnectionString}");

I hope this helps in many Projects :)

Please feel free to comment or suggest features.

easyxmlserializer's People

Contributors

digatodiga avatar itagnesmeyer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.