xml - Serialize in C# - per variable attributes associated with a new XmlElelemt -
i've posted because of similar case time ago. i'm @ next port, unfortunately bit complicated. problem transform class xml in format must be:
<entities> <entity name="s40"> <attributes> <attribute name="insobject">x</attribute> <attribute name="insobjecttyp">x</attribute> </attributes> </entity> <entity name="s41"> <attributes> <attribute name="insobez">x</attribute> <attribute name="/sde/cd_fdate">x</attribute> <attribute name="/sde/cd_col_form">x</attribute> <attribute name="/sde/cd_applnr">x</attribute> <attribute name="/sde/cd_polbeg">x</attribute> </attributes> </entity> the problem 1 here looks nice, each variable converted xml format @ same time needs new xmlelement name "attributes". since can create new item attribute, thought of string.replace ... unfortunately, have problem , can not replaced, since there several distinct it. unfortunately, can switch xml format nothing, because format given me , gets used others.
my way now:
[system.xml.serialization.xmlroot("entities")] public class ivrequester { private s_40[] s40; private s_41[] s41; private s_42[] s42; private s_43[] s43; private s_45[] s45; private c_99[] c99; public s_40[] s40 { { return s40; } set { s40 = value; } } public s_41[] s41 { { return s41; } set { s41 = value; } } public s_42[] s42 { { return s42; } set { s42 = value; } } public s_43[] s43 { { return s43; } set { s43 = value; } } public s_45[] s45 { { return s45; } set { s45 = value; } } public c_99[] c99 { { return c99; } set { c99 = value; } } public class s_40 { public string insobject = "x"; public string insobjecttyp = "x"; public string cd_oldobjnr = "x"; } public class s_41 { public string insobez = "x"; public string slashsdeslashcd_fdate = "x"; public string slashsdeslashcd_col_form = "x"; public string slashsdeslashcd_applnr = "x"; } public class s_42 { public string partner = "x"; public string partner_ext = "x"; public string partner_adext = "x"; } public class s_43 { public string partner = "x"; } public class s_45 { public string partner = "x"; public string partner_ext = "x"; public string partner_adext = "x"; } public class c_99 { public string code = "x"; public string mv_message = "x"; public string mv_msgty = "x"; } public string xmlout() { string returnstring = program.objecttoxml(this); console.writeline(returnstring); string[] toreplace = {"insobject", "insobjecttyp", "cd_oldobjnr", "cd_branch", "partner", "partner_ext", "partner_adext", ... }; (int = 0; < toreplace.length; i++) { returnstring = returnstring.replace("<" + toreplace[i] + ">", "<attribute name=\"" + toreplace[i] + "\">"); returnstring = returnstring.replace("</" + toreplace[i] + ">", "</attribute>"); returnstring = returnstring.replace("<" + toreplace[i] + "/>", "<attribute name=\"" + toreplace[i] + "\"/>"); returnstring = returnstring.replace("<" + toreplace[i] + " />", "<attribute name=\"" + toreplace[i] + "\"/>"); } returnstring = returnstring.replace("slashsdeslash", "/sde/"); returnstring = returnstring.replace("<entities xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">", "<entities>"); string[] entitiestoreplace = { "s40", "s41", "s42", "s43", "s45", "c99" }; (int = 0; < entitiestoreplace.length; i++) { returnstring = returnstring.replace("<" + entitiestoreplace[i] + ">", "<entity name=\"" + entitiestoreplace[i] + "\">"); returnstring = returnstring.replace("</" + entitiestoreplace[i] + ">", "</entity>"); returnstring = returnstring.replace("<" + entitiestoreplace[i] + "/>", "<entity name=\"" + entitiestoreplace[i] + "\"/>"); returnstring = returnstring.replace("<" + entitiestoreplace[i] + " />", "<entity name=\"" + entitiestoreplace[i] + "\"/>"); } string[] changetoattributes = { "s_40", "s_41", "s_42", "s_43", "s_45", "c_99" }; (int = 0; < changetoattributes.length; i++) { returnstring = returnstring.replace("<" + changetoattributes[i] + ">", "<attributes>"); returnstring = returnstring.replace("</" + changetoattributes[i] + ">", "</attributes>"); returnstring = returnstring.replace("<" + changetoattributes[i] + "/>", "<attributes/>"); returnstring = returnstring.replace("<" + changetoattributes[i] + " />", "<attributes/>"); } return returnstring; } public ivrequester xmlin(string xmlcode) { console.writeline(xmlcode); string returnstring = xmlcode; string[] toreplace = { "insobject", "insobjecttyp", "cd_oldobjnr", "cd_branch", "partner", "partner_ext", "partner_adext", "valid_from", "aend_tms", "function", "username", "hostuser", "source", "insobez", ... }; (int = 0; < toreplace.length; i++) { returnstring = returnstring.replace("<attribute name=\"" + toreplace[i] + "\">", "<" + toreplace[i] + ">"); returnstring = returnstring.replace("</attribute>", "</" + toreplace[i] + ">"); returnstring = returnstring.replace("<attribute name=\"" + toreplace[i] + "\"/>", "<" + toreplace[i] + "/>"); returnstring = returnstring.replace("<attribute name=\"" + toreplace[i] + "\"/>", "<" + toreplace[i] + " />"); } returnstring = returnstring.replace("/sde/", "slashsdeslash"); returnstring = returnstring.replace("<entities>", "<entities xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">"); string[] entitiestoreplace = { "s40", "s41", "s42", "s43", "s45", "c99" }; (int = 0; < entitiestoreplace.length; i++) { returnstring = returnstring.replace("<entity name=\"" + entitiestoreplace[i] + "\">", "<" + entitiestoreplace[i] + ">"); returnstring = returnstring.replace("</entity>", "</" + entitiestoreplace[i] + ">"); returnstring = returnstring.replace("<entity name=\"" + entitiestoreplace[i] + "\"/>", "<" + entitiestoreplace[i] + "/>"); returnstring = returnstring.replace("<entity name=\"" + entitiestoreplace[i] + "\"/>", "<" + entitiestoreplace[i] + " />"); } ivrequester test = new ivrequester();//<--search better way that... want give return, coming next or somthing else ;) try { xmlserializer serializer = new xmlserializer(this.gettype()); test = (ivrequester)serializer.deserialize(new stringreader(returnstring)); //<--search better way that... want give return, coming next or somthing else ;) } catch (exception ex) { console.writeline(ex.message); return test; } return test; } i've tried before [xmlattribute ()] , [xmlelement ()], unfortunately got no further that. when time not scarce string.replace absolutely not suitable that, time thought myself when print anyway specified <...> can not happen ... when converting xml no problem, xml code classes, unfortunately, not feasible because financial statements have same name ( , ). either there way serialize (but apparently there not) xmlelement attribute products, or need better xml parser, built since nodes can not rename this. -.-
i hope has tip me, szandor
ps: i've tried class called attribute, , had put name of attribute name , value. unfortunately, not work when second attributes time set xmlelement, it'll crash ... second when attribute2 call, goes, since hot must attribute, unfortunately unusable :(
public class attribute { [xmlattribute("name")] public string name; [xmltext()] public string wert; } public class s_40 { [xmlelement("attribute")] public attribute insobject2 = new attribute() { name = "insobject", wert = "test" }; [xmlelement("attribute")] public attribute insobjecttyp2 = new attribute() { name = "insobjecttyp", wert = "test" }; ... }
have been solved in way not code , works :)
xmldocument originalxml = new xmldocument(); xmldocument convertedxml = new xmldocument(); originalxml.loadxml(xmlcode); convertedxml.loadxml("<entities xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\"></entities>"); xmlnode rootnodeoriginal = originalxml.selectsinglenode("/entities"); xmlnode rootnodeconverted = convertedxml.selectsinglenode("/entities"); string entityname = ""; string entityname2 = ""; foreach (xmlnode entitynodes in rootnodeoriginal.childnodes) { if (entitynodes.name == "entity") { entityname = entitynodes.attributes[0].innerxml; entityname2 = entityname.replace("s","s_").replace("c","c_"); xmlnode newnode = convertedxml.createelement(entityname); rootnodeconverted.appendchild(newnode); convertedxml.selectsinglenode("/entities/" + entityname).appendchild(convertedxml.createelement(entityname2)); if (entitynodes.firstchild.name == "attributes") foreach (xmlnode childnodetochange in entitynodes.firstchild.childnodes) if (childnodetochange.name == "attribute") { xmlnode newchildnodes = convertedxml.createelement(childnodetochange.attributes[0].innerxml.replace("/","slash")); newchildnodes.innerxml = childnodetochange.innerxml; convertedxml.selectsinglenode("/entities/" + entityname + "/" + entityname2).appendchild(newchildnodes); } } }
Comments
Post a Comment