在运行时C#上向XElment添加参数

时间:2014-07-10 04:51:01

标签: c# .net xml functional-programming xelement

我正在通过属性集合创建XML String,但我遇到了一件事。我想在运行时根据值是否存在添加不同的XElement参数。在下面的代码中,我通过添加新的XElement(“Temp_F”,Value)向XElement的构造函数发送不同的参数,但我想添加条件,如果我的值为null或为空,则不创建和添加XElememt参数。

var xmlDoc = new XDocument();
        xmlDoc.Add(new XElement("root"));
        if (prescriptionTemperatureList != null && prescriptionTemperatureList.Count > 0)
            {
            foreach (var medicationEntity in prescriptionTemperatureList)
                {
                if (xmlDoc.Root != null)
                    {
                    xmlDoc.Root.Add(
                        new XElement("Temprature",
                                     new XElement("Temp_F", !string.IsNullOrEmpty(medicationEntity.Value) ?  medicationEntity.Value : "0"),
                                     new XElement("Temp_C", !string.IsNullOrEmpty(medicationEntity.Value) ? ((Convert.ToInt64(medicationEntity.Value)-32)/1.80).ToString() : "0"),
                                     new XElement("vHour", !string.IsNullOrEmpty(medicationEntity.Time) ? (DateTime.Parse(medicationEntity.Time).Hour).ToString() : ""),
                                     new XElement("vMin",!string.IsNullOrEmpty(medicationEntity.Time) ?  (DateTime.Parse(medicationEntity.Time).Minute).ToString() : ""),
                                     new XElement("vEvent",!string.IsNullOrEmpty(medicationEntity.Time) ?  DateTime.Parse(medicationEntity.Time).Hour> 11 ? "pm" : "am" :""),
                                     new XElement("Temp_Method", medicationEntity.Method),
                                     new XElement("Vsdate", vsDate)

                            ));
                    }
                newTempratureList.Add(medicationEntity);
                }
            }

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

xmlDoc.Root.Add(
    new XElement("Temprature",
        new[]
        {
            new XElement("Temp_F",
                !string.IsNullOrEmpty(medicationEntity.Value) ? medicationEntity.Value : null),
            new XElement("Temp_C",
                !string.IsNullOrEmpty(medicationEntity.Value)
                    ? ((Convert.ToInt64(medicationEntity.Value) - 32)/1.80).ToString()
                    : null),
            new XElement("vHour",
                !string.IsNullOrEmpty(medicationEntity.Time)
                    ? (DateTime.Parse(medicationEntity.Time).Hour).ToString()
                    : null),
            new XElement("vMin",
                !string.IsNullOrEmpty(medicationEntity.Time)
                    ? (DateTime.Parse(medicationEntity.Time).Minute).ToString()
                    : null),
            new XElement("vEvent",
                !string.IsNullOrEmpty(medicationEntity.Time)
                    ? DateTime.Parse(medicationEntity.Time).Hour > 11 ? "pm" : "am"
                    : null)
        }.Where(w => !string.IsNullOrWhiteSpace(w.Value)),
        new XElement("Temp_Method", medicationEntity.Method),
        new XElement("Vsdate", vsDate)
        ));

答案 1 :(得分:1)

  

" ..我想添加条件,如果我的值为null或为空,则不创建和添加XElememt 参数。"

所以请添加条件检查,这种方法有什么问题吗? :

var xmlDoc = new XDocument();
xmlDoc.Add(new XElement("root"));
if (prescriptionTemperatureList != null && prescriptionTemperatureList.Count > 0)
{
    foreach (var medicationEntity in prescriptionTemperatureList)
    {
        if (xmlDoc.Root != null)
        {
            var temperature = new XElement("Temprature");
            if(!string.IsNullOrEmpty(medicationEntity.Value))
                temperature.Add(new XElement("Temp_F", medicationEntity.Value));
            if(!string.IsNullOrEmpty(medicationEntity.Value))
                temperature.Add(new XElement("Temp_C", ((Convert.ToInt64(medicationEntity.Value)-32)/1.80).ToString()));
            if(!string.IsNullOrEmpty(medicationEntity.Time))
                temperature.Add(new XElement("vHour", (DateTime.Parse(medicationEntity.Time).Hour).ToString()));
            .........
            .........
            xmlDoc.Root.Add(temperature);
        }
        newTempratureList.Add(medicationEntity);
    }
}