vendredi 31 juillet 2015

split string in single lines and save the data

I try to split array in single lines and save it to xml file. It is about IpAddress. You can put some strings under each other. But now it is one 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);
            for (int i = 0; i < lines.Length; i++) {
                if ( lines[i] != null){
                lines[i] = condition.Value.ToString();
            }
                //lines = string.Join("\n", condition.Value.ToArray());
            }


        }
    }

it is about IPaddress.

But the problem is lines is always null. Thank you.

This is the IpAddress:

 [Display(Name = "PreConditionIpAddress", ResourceType = typeof(Resources.Entity.Product))]
        public string IpAddress { get; set; }

This is my 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 it now like 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 the output is: 777777 99999999 8888888 .

But it has to be:

<ipaddress>77777777</ipaddress>
<ipaddress>99999999999</ipaddress>
<ipaddress>888888888888</ipaddress>

Thank you

Aucun commentaire:

Enregistrer un commentaire