保存由新行分隔并以空格分隔的值

时间:2015-08-06 07:28:05

标签: c# asp.net-mvc

我尝试从文本框中保存值。像这样:

  1. 777 888 111 222
  2. 然后必须像这样保存值:

    <ipaddresses>
        <ipaddress>777</ipaddress>
        <ipaddress>888</ipaddress>
        <ipaddress>111</ipaddress>
        <ipaddress>222</ipaddress>
    </ipaddresses>
    

    但现在我把它作为输出:

    <ipaddresses>
        <ipaddress>11</ipaddress>
        <ipaddress>22</ipaddress>
        <ipaddress>33</ipaddress>
    </ipaddresses>
    <ipaddresses>
        <ipaddress>11</ipaddress>
        <ipaddress>22</ipaddress>
    </ipaddresses>
    <ipaddresses>
        <ipaddress>11</ipaddress>
        <ipaddress>22</ipaddress>
        <ipaddress>33</ipaddress>
    </ipaddresses>
    

    我这样试试:

    internal string Serialize(EditProductModel model)
    {
        XElement settings = XElement.Parse(model.Product.AuthenticationSettings ?? "<settings/>");
        XElement ipaddresses = new XElement("ipaddresses");
    
        if (IpAddress == null)
           return string.Empty;                 
    
        string[] lines = IpAddress.Split(new string[] { Environment.NewLine,  }, StringSplitOptions.RemoveEmptyEntries);
        lines = IpAddress.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
        foreach (var item in lines)
        {
            if (item.StartsWith("127") || item.StartsWith("192"))
                //return  ("LogOnDetails", model);
                return model.Product.AuthenticationSettings;
    
                        IPAddress address;// = null;
                        if (IPAddress.TryParse(item, out address))
                        {
    
                            switch (address.AddressFamily)
                            {
                                case System.Net.Sockets.AddressFamily.InterNetwork:
                                    ipaddresses.Add(new XElement("ipaddress", item));
                                    // we have IPv4
                                    break;
                                case System.Net.Sockets.AddressFamily.InterNetworkV6:
                                    // we have IPv6
                                    ipaddresses.Add(new XElement("ipaddress", item));
                                    break;
                                default:
                                    break;
                            }
                        }
                        settings.Add(ipaddresses);
                    }
    
                return settings.ToString();
            }
    

    谢谢。如果有人有任何建议。谢谢

    问题是如果我这样做:

    1. 11 99 22 33
    2. 88
    3. 99
    4. 然后只有这个值:11 99 22将被保存

      如果我这样做:

      1. 1 2 3
      2. 4 5
      3. 然后只有数字:

        <ipaddress>1</ipaddress>
            <ipaddress>2</ipaddress>
            <ipaddress>5</ipaddress>
        

        将被保存

        这就是诀窍:

          lines = IpAddress.Split(new char[0]);
        

1 个答案:

答案 0 :(得分:2)

您需要移动此行

settings.Add(ipaddresses);

for循环之外。 e.g。

foreach (var item in lines)
{
    //... Rest of loop code.
}

settings.Add(ipaddresses);

XML应如下所示:

<settings>
  <ipaddresses>
    <ipaddress>11</ipaddress>
    <ipaddress>22</ipaddress>
    <ipaddress>33</ipaddress>
  </ipaddresses>
</settings>