分割字符串并保存到xml [英] splitting string and save to xml

查看:39
本文介绍了分割字符串并保存到xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本字段中,您可以输入字符串(数字),如下所示:78787.88888777.99999

In a textfield you can enter strings(numbers), like this: 78787.8888 8777.99999

并且必须按如下方式保存值:

and the values has to be saved like this:

<ipaddresses>
<ipaddress>78787.8888  </ipaddress>
<ipaddress>8777.99999  </ipaddress>
</ipaddresses>

我正在为此使用序列化方法,如下所示:

I am using for this a serialize method, like this:

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


            XElement ipaddresses = new XElement("ipaddresses");
            string[] lines = ipaddresses.Value.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            foreach (var item in lines) {
                lines.Select(ip=> new XElement("item", ip)).ToString();

            }




            settings.Add(ipaddresses);


            return settings.ToString();
        }

但是每次行都是".

这是属性:

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

谢谢.

好吧,我现在这样子:

 XElement ipaddresses = new XElement("ipaddresses");
            string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in lines) {   

               // lines.Select(ip => new XElement("ipaddress", ip)).ToString();
                settings.Add(new XElement("ipaddress", item));

            }

我得到:

<ipaddress>78787.8888  </ipaddress>
<ipaddress>8777.99999  </ipaddress>

但是如何获得它:

<ipaddresses>
<ipaddress>78787.8888  </ipaddress>
<ipaddress>8777.99999  </ipaddress>
</ipaddresses>

谢谢

推荐答案

您创建了 ipaddresses 节点,但是直接在设置下添加了 ipaddress 注释.因此,如果您首先将节点添加到 ipaddresses 中,然后将该节点添加到父节点中,它将正常工作.像这样:

You created ipaddresses node but you're adding the ipaddress notes directly under settings. So if you add the nodes to ipaddresses first then add that node to parent it should work. Something like this:

XElement ipaddresses = new XElement("ipaddresses");
string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in lines) 
{  
    ipaddresses.Add(new XElement("ipaddress", item));
}

settings.Add(ipaddresses);

这篇关于分割字符串并保存到xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆