c# - split string in single lines and save the data -
i try split array in single lines , save xml file. ipaddress. can put strings under each other. 1 long string
i have this:
internal void deserialize(product product) { xelement settings = xelement.parse(product.authenticationsettings ?? "<settings/>"); if (settings == null || settings.attribute("authenticationrequired") == null || settings.attribute("authenticationrequired").value != "true") return; xelement conditions = settings.element("preconditions"); if (conditions == null) return; //xelement[] conditions2 = settings.element[("preconditions")]; //if (conditions == null) // return; xelement condition = conditions.element("residentsonly"); if (condition!= null) this.residentsonly = (condition.value == "1"); condition = conditions.element("minimumage"); if (condition != null) { int age = 0; if (int32.tryparse(condition.value, out age)) this.minimumage = age; } condition = conditions.element("redirecturl"); if (condition != null) { this.redirecturl = condition.value; } condition = conditions.element("ipaddress"); if (condition != null) { string[] lines = ipaddress.split(new string[] { environment.newline }, stringsplitoptions.none); //condition = ipaddress.split(new string[] { environment.newline }, stringsplitoptions.none); (int = 0; < lines.length; i++) { if ( lines[i] != null){ lines[i] = condition.value.tostring(); } //lines = string.join("\n", condition.value.toarray()); } } }
it ipaddress.
but problem lines null. thank you.
this ipaddress:
[display(name = "preconditionipaddress", resourcetype = typeof(resources.entity.product))] public string ipaddress { get; set; }
this serialize method:
internal string serialize(editproductmodel model) { if (this.residentsonly == false && this.minimumage == 0) return model.product.authenticationsettings; xelement settings = xelement.parse(model.product.authenticationsettings ?? "<settings/>"); if (settings == null || settings.attribute("authenticationrequired") == null || settings.attribute("authenticationrequired").value != "true") return model.product.authenticationsettings; settings.add( new xelement("preconditions", new xelement("residentsonly", this.residentsonly ? "1" : "0"), new xelement("minimumage", this.minimumage), new xelement("redirecturl", this.redirecturl), new xelement("ipaddress", this.ipaddress) ) ); return settings.tostring(); }
i have this:
public class preconditionsettings { [display(name = "preconditionresidentsonly", resourcetype = typeof(resources.entity.product))] public bool residentsonly { get; set; } [display(name = "preconditionminimumage", resourcetype = typeof(resources.entity.product))] public int minimumage { get; set; } [sfshelpers.preconditionredirectvalidation(errormessageresourcetype = typeof(resources.entity.product), errormessageresourcename="preconditionredirecturlvalidation")] [display(name = "preconditionredirecturl", resourcetype = typeof(resources.entity.product))] public string redirecturl { get; set; } [display(name = "preconditionipaddress", resourcetype = typeof(resources.entity.product))] public string ipaddress { get; set; } public preconditionsettings() { this.residentsonly = false; this.minimumage = 0; this.redirecturl = null; this.ipaddress = null; } public static string[] replacer(dictionary<string, string> dic, string ipaddress) { foreach (keyvaluepair<string, string> entry in dic) { ipaddress = ipaddress.replace(entry.key, entry.value); } string[] lines = ipaddress.split(new string[] { environment.newline }, stringsplitoptions.none); return lines; } internal string serialize(editproductmodel model) { if (this.residentsonly == false && this.minimumage == 0) return model.product.authenticationsettings; xelement settings = xelement.parse(model.product.authenticationsettings ?? "<settings/>"); if (settings == null || settings.attribute("authenticationrequired") == null || settings.attribute("authenticationrequired").value != "true") return model.product.authenticationsettings; settings.add( new xelement("preconditions", new xelement("residentsonly", this.residentsonly ? "1" : "0"), new xelement("minimumage", this.minimumage), new xelement("redirecturl", this.redirecturl), new xelement("ipaddress", this.ipaddress) ) ); return settings.tostring(); } internal void deserialize(editproductmodel model) { deserialize(model.product); } internal void deserialize(product product) { xelement settings = xelement.parse(product.authenticationsettings ?? "<settings/>"); if (settings == null || settings.attribute("authenticationrequired") == null || settings.attribute("authenticationrequired").value != "true") return; xelement conditions = settings.element("preconditions"); if (conditions == null) return; xelement condition = conditions.element("residentsonly"); if (condition!= null) this.residentsonly = (condition.value == "1"); condition = conditions.element("minimumage"); if (condition != null) { int age = 0; if (int32.tryparse(condition.value, out age)) this.minimumage = age; } condition = conditions.element("redirecturl"); if (condition != null) { this.redirecturl = condition.value; } condition = conditions.element("ipaddress"); if (condition != null) { ipaddress = ""; dictionary<string, string> dic = new dictionary<string, string>(); dic.add("<ipaddress>", string.empty); dic.add("</ipaddress>", string.empty); dic.add(" ", environment.newline); replacer(dic, ipaddress); this.ipaddress = condition.value; } } }
now output is: 777777 99999999 8888888 .
but has be:
<ipaddress>77777777</ipaddress> <ipaddress>99999999999</ipaddress> <ipaddress>888888888888</ipaddress>
thank you
how this?
here's method call:
string somedata = ""; dictionary<string, string> dic = new dictionary<string, string>(); dic.add("<ipaddress>", string.empty); dic.add("</ipaddress>", string.empty); dic.add(" ", environment.newline); string[] data = replacer(dic, somedata); data = wrapper(data, "ipaddress"); string result = combiner(data);
here's method itself:
public static string[] replacer(dictionary<string,string> dic, string data) { foreach (keyvaluepair<string,string> entry in dic) { data = data.replace(entry.key, entry.value); } string[] delimiters = new string[] { environment.newline, " " }; string[] lines = data.split(delimiters ,stringsplitoptions.removeemptyentries); return lines; } public static string[] wrapper(string[] data, string tag) { string tagopen, tagclose; tagopen = "<" + tag + ">"; tagclose = "</" + tag + ">"; for(int = 0; < data.length; i++) { data[i] = tagopen + data[i] + tagclose; } return data; } public static string combiner(string[] data) { string res = string.empty; for(int = 0; < data.length; i++) { res += data[i] + environment.newline; } return res; }
Comments
Post a Comment