what best proceed serialize xml suitable objects versioned namespaces?


my doubt following.



i have xml versioned namespace. i wish serialize viewed xml suitable object, nonetheless wholly proceed i know means i have slight xml twin times. initial learn namespace, following method serialize vigilant repremand form formed detected namespace. seems dreadfully ruin me, there contingency proceed controlling generics something suitable form vigilant behind but an 'if namespace == x following serialze that' check.



below illustration wholly proceed i know accomplish this. there improved some-more fit way?



thanks



using system;
using system.text;
using system.collections.generic;
using system.linq;
using microsoft.visualstudio.testtools.unittesting;
using system.xml.linq;
using system.xml;
using system.xml.serialization;
using system.io;

namespace testproject1
{
[testclass]
open category unittest1
{
[testmethod]
open vacant testmethod3()
{
//build adult an worker vigilant xml
schema.v2.employee worker = new schema.v2.employee { firstname = "first", lastname = "last" };
twine xml = employee.objecttoxml<schema.v2.employee>();

//now feign i don't know form i am receiving.
twine namespace = getnamespace(xml);
vigilant newemp;
(namespace == "employee.v2")
newemp = xmlserializationextension.xmltoobject<schema.v2.employee>(null, xml);
else
newemp = xmlserializationextension.xmltoobject<schema.v1.employee>(null, xml);

// check certain form i got i made.
assert.areequal(typeof(schema.v2.employee), newemp.gettype());
}

open twine getnamespace(string s)
{
xdocument z = xdocument.parse(s);
var outcome = z.root.attributes().
where(a => a.isnamespacedeclaration).
groupby(a => a.name.namespace == xnamespace.none ? string.empty : a.name.localname,
=> xnamespace.get(a.value)).
todictionary(g => g.key,
g => g.first());

foreach (system.xml.linq.xnamespace vigilant result.values)
(item.namespacename.contains("employee")) relapse item.namespacename;

relapse string.empty;
}
}

open stationary category xmlserializationextension
{
open stationary twine objecttoxml<t>(this t object)
{
xmlserializer s = new xmlserializer(object.gettype());
controlling (stringwriter author = new stringwriter())
{
s.serialize(writer, object);
relapse writer.tostring();
}
}
open stationary t xmltoobject<t>(this t object, twine xml)
{
xmlserializer s = new xmlserializer(typeof(t));
controlling (stringreader reader = new stringreader(xml))
{
vigilant obj = s.deserialize(reader);
relapse (t)obj;
}
}
}
}

namespace schema.v1
{
[xmlroot(elementname = "employee", namespace= "employee.v1", isnullable = false)]
open category employee
{
[xmlelement(elementname = "firstname")]
open twine firstname { get; set; }
[xmlelement(elementname = "lastname")]
open twine lastname { get; set; }
}
}
namespace schema.v2
{
[xmlroot(elementname = "employee", namespace = "employee.v2", isnullable = false)]
open category employee
{
[xmlattribute(attributename = "firstname")]
open twine firstname { get; set; }
[xmlattribute(attributename = "lastname")]
open twine lastname { get; set; }
}
}


Comments

Popular posts from this blog

list macos calm editors formula editors

how hibernate @any-related annotations?

why does floated <input> control floated component slip over too distant right ie7, nonetheless firefox?