将字符串拆分为单行并保存数据

时间:2015-07-30 09:33:51

标签: c# asp.net-mvc

我尝试将数组拆分为单行并将其保存到xml文件中。这是关于IpAddress。你可以把一些字符串放在彼此之下。但现在它是一个长字符串

我有这个:

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());
            }


        }
    }

是关于IPaddress。

但问题是线条始终为空。谢谢。

这是IpAddress:

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

这是我的序列化方法:

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();
        }

我现在就这样:

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;


            }
        }
    }

现在的输出是:777777 99999999 8888888

但必须是:

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

谢谢

1 个答案:

答案 0 :(得分:1)

这个怎么样?

这是方法调用:

        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);

这是方法本身:

    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 i = 0; i < data.Length; i++)
   {
       data[i] = tagOpen + data[i] + tagClose;
   }

   return data;
}


public static string combiner(string[] data)
{
   string res = string.Empty;
   for(int i = 0; i < data.Length; i++)
   {
       res += data[i] + Environment.NewLine;
   }

   return res;
}