Serialize in C# - per variable attributes associated with a new XmlElelemt

Prakash

New Member
I've posted because of a similar case some time ago. Now I'm at the next port, where it is now unfortunately a bit complicated. The problem is that I completely transform a class into XML in this format must be:\[code\]<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>\[/code\]The problem with this is the one here looks nice, for each variable is converted into XML format at the same time needs a new XmlElement with the name "ATTRIBUTES". But since I can create a new item WITH attribute, I thought of a String.Replace ... Unfortunately, I have but the problem is the and can not be easily replaced, since there are several distinct from it. Unfortunately, I can also switch to the XML format nothing, because this format is given to me and it gets so used already for others.My way now:\[code\][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 { get { return s40; } set { s40 = value; } } public s_41[] S41 { get { return s41; } set { s41 = value; } } public s_42[] S42 { get { return s42; } set { s42 = value; } } public s_43[] S43 { get { return s43; } set { s43 = value; } } public s_45[] S45 { get { return s45; } set { s45 = value; } } public c_99[] C99 { get { 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", ... }; for (int i = 0; i < toreplace.Length; i++) { returnstring = returnstring.Replace("<" + toreplace + ">", "<ATTRIBUTE name=\"" + toreplace + "\">"); returnstring = returnstring.Replace("</" + toreplace + ">", "</ATTRIBUTE>"); returnstring = returnstring.Replace("<" + toreplace + "/>", "<ATTRIBUTE name=\"" + toreplace + "\"/>"); returnstring = returnstring.Replace("<" + toreplace + " />", "<ATTRIBUTE name=\"" + toreplace + "\"/>"); } 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" }; for (int i = 0; i < EntitiesToReplace.Length; i++) { returnstring = returnstring.Replace("<" + EntitiesToReplace + ">", "<ENTITY name=\"" + EntitiesToReplace + "\">"); returnstring = returnstring.Replace("</" + EntitiesToReplace + ">", "</ENTITY>"); returnstring = returnstring.Replace("<" + EntitiesToReplace + "/>", "<ENTITY name=\"" + EntitiesToReplace + "\"/>"); returnstring = returnstring.Replace("<" + EntitiesToReplace + " />", "<ENTITY name=\"" + EntitiesToReplace + "\"/>"); } string[] ChangeToAttributes = { "s_40", "s_41", "s_42", "s_43", "s_45", "c_99" }; for (int i = 0; i < ChangeToAttributes.Length; i++) { returnstring = returnstring.Replace("<" + ChangeToAttributes + ">", "<ATTRIBUTES>"); returnstring = returnstring.Replace("</" + ChangeToAttributes + ">", "</ATTRIBUTES>"); returnstring = returnstring.Replace("<" + ChangeToAttributes + "/>", "<ATTRIBUTES/>"); returnstring = returnstring.Replace("<" + ChangeToAttributes + " />", "<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", ... }; for (int i = 0; i < toreplace.Length; i++) { returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace + "\">", "<" + toreplace + ">"); returnstring = returnstring.Replace("</ATTRIBUTE>", "</" + toreplace + ">"); returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace + "\"/>", "<" + toreplace + "/>"); returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace + "\"/>", "<" + toreplace + " />"); } 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" }; for (int i = 0; i < EntitiesToReplace.Length; i++) { returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace + "\">", "<" + EntitiesToReplace + ">"); returnstring = returnstring.Replace("</ENTITY>", "</" + EntitiesToReplace + ">"); returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace + "\"/>", "<" + EntitiesToReplace + "/>"); returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace + "\"/>", "<" + EntitiesToReplace + " />"); } ivRequester test = new ivRequester();//<--Search for better way to do that... i want to give THIS as return, but that is coming next or somthing else ;) try { XmlSerializer serializer = new XmlSerializer(this.GetType()); test = (ivRequester)serializer.Deserialize(new StringReader(returnstring)); //<--Search for better way to do that... i want to give THIS as return, but that is coming next or somthing else ;) } catch (Exception ex) { Console.WriteLine(ex.Message); return test; } return test; }\[/code\]I've tried before with [XmlAttribute ()] and [XmlElement ()], but unfortunately I got no further with anything like that. Would when the time was not so scarce String.Replace say is absolutely not suitable for something like that, but by the time I thought to myself when I print everything anyway SPECIFIED with <...> can not happen ... When converting to XML no problem, but the XML code back into the classes, unfortunately, is not feasible because the financial statements all have the same name ( and ).So either there is a way to serialize what (but apparently there is not) an XmlElement WITH ATTRIBUTE products, or do I need a better XML parser, as is the built since nodes can not even rename this. -.-I hope someone has a tip for me, SzandorPS: I've tried also with an extra class called ATTRIBUTE, and then you had to put the name of the attribute name and value. Unfortunately, this does not work when a second ATTRIBUTES time was set as XmlElement, it'll crash ... the second when I Attribute2 call, it goes, but since all the HOT MUST ATTRIBUTE, unfortunately unusable :(\[code\]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" }; ...}\[/code\]
 
Back
Top