迭代XElement

时间:2015-08-05 06:54:35

标签: c# asp.net-mvc

我正在尝试迭代XElement项,但我只看到一个值,而不是其他值。

我正在使用序列化和反序列化。

对于序列化我正在使用它:

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


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

    return settings.ToString();
}

关于Ipaddress。因此它保存了正确的值,例如:

<ipaddresses>
  <ipaddress>12</ipaddress>
  <ipaddress>23</ipaddress>
</ipaddresses>

然后我想在文本字段中表达和显示值。我是这样做的:

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 ipIpAddresses = settings.Element("ipaddresses");
    if (ipIpAddresses == null)
        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;
    }

    if (ipIpAddresses != null)
    {
        foreach (XElement childElement in ipIpAddresses.Elements("ipaddress"))
        {
          this.IpAddress = childElement.Value;  //conditionIP.Value;
        }               
    }
}

and it is about this code fragment:

if (ipIpAddresses != null)
{
    foreach (XElement childElement in ipIpAddresses.Elements("ipaddress"))
    {
      this.IpAddress = childElement.Value;  //conditionIP.Value;
    }               
}

我在childElement.value中看到了值,但在文本字段中我只看到了值23

ipAddress的属性是:

public string IpAddress { get; set; }

这是IpAddress的视图:

<div class="form-group">
    @Html.LabelFor(model => model.PreCondition.IpAddress, new { @class = "text-bold control-label col-md-3" })
    <div class="col-lg-6 col-md-8 col-sm-10 ">
        @Html.TextAreaFor(model => model.PreCondition.IpAddress, new { htmlAttributes = new { @class = "form-control col-md-3" } })
    </div>
</div>

更新

我现在这样做:

this.IpAddress += childElement.Value;

但他们现在不在彼此之下。

1 个答案:

答案 0 :(得分:0)

哦,我这样解决了:

  this.IpAddress += childElement.Value + "\n";