c# - How can I convert object to strongly typed XML -
is there way convert object xml each element contains attribute (let's xt:type="int").
know can manually using reflection , iterate properties , ....
i'm asking if there's way generate using pre-made library or something.
i'm doing is:
xmldocument doc = new xmldocument(); xmlserializer ser = new xmlserializer(document.gettype()); string result = string.empty; using (memorystream memstm = new memorystream()) { ser.serialize(memstm, document); memstm.position = 0; result = new streamreader(memstm).readtoend(); }
because later need read object. want programmatically, not using xsd tool.
thanks
update 1:
want looks this:
<note> <propertyname1 xs:type="string">value1</to> <propertyname2 xs:type="int">10</to> <propertyname2 xs:type="datetime">04-06-2015 01:10:00</to> </note>
the important attribute xs:type.
if use system.xml.linq, can add xattribute whatever want in it.
lets have personn p string name , int age
xelement e = new xelement( "personne" ); xelement age = new xelement("age",personne.age); age.add(new xattribute("xs:type", typeof(personne.age))); e.add(age); xelement name = new xelement("name",personne.name); name.add(new xattribute("xs:type", typeof(personne.name))); e.add(name);`
you receive xml:
<personne> <age type="int">(the age of personne)</age> <name type="string">(he name of personne)</name> <personne>
with "automated" process:
xelement p = new xelement( "personne" ); foreach(var property in personn.gettype().getproperties()) { xelement e = new xelement(prop.name, prop.getvalue(p, null)); e.add(new xattribute("xs:type", typeof(personne.prop))); p.add(e); }
edited: added "xs:" need it
Comments
Post a Comment