我有一个文本字段,用户可以在其中填写字符串。但是现在如何保存不同的字符串。因为现在它是一个长串。而不是分开的字符串 这是Serialize和Deserialize的类:
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;
}
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) {
this.IpAddress = condition.Value;
}
}
它是关于属性:IpAddress。输出结果如下:
<settings authenticationrequired="true">
<accesslevel>level10</accesslevel>
<profile>test</profile>
<preconditions>
<residentsonly>1</residentsonly>
<minimumage>55</minimumage>
<redirecturl>/page/newpage</redirecturl>
<ipaddress>88888888
999999999</ipaddress>
</preconditions>
</settings>
但是你看到它是一个字符串,而不是两个字符串:88888888和999999999。
谢谢
我这样试试:
condition = conditions.Element("ipaddress");
if (condition != null) {
string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
this.IpAddress = condition.Value;
}
我尝试这样的事情:
condition = conditions.Element("ipaddress");
if (condition != null) {
string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string lines = string.Join("\n",condition.Value);
//this.IpAddress = condition.Value;
}
如果我试试这个:
condition = conditions.Element("ipaddress");
if (condition != null) {
string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++) {
lines[i] = condition.Value.ToString();
//lines = string.Join("\n", condition.Value.ToArray());
}
//lines = string.Join("\n",condition.Value);
//this.IpAddress = condition.Value;
}
我收到此错误:
Object reference not set to an instance of an object.
在这一行:
string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
答案 0 :(得分:1)
您应该将字符串IpAddress拆分为\ n或Environment.NewLine并保存为字符串数组。
编辑后:
condition = conditions.Element("ipaddress");
if (condition != null) {
string[] lines = condition.Value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
this.IpAddress = new XElement("ipaddresses", lines.Select(o=> new XElement("item", o))).ToString();
}
答案 1 :(得分:0)
我建议您将属性更改为ICollection或创建自定义类型IpAddresses,实现ICollection和自定义序列化/业务逻辑。
这会给您的xml文件带来以下更改:
<ipaddresses>
<item>88888888</item>
<item>99999999</item>
</ipaddresses>