0

I am creating a XML String by collection of attributes but i struck in one thing. I want to add different XElement Parameter at runtime on the basis of value exist or not. In the following code i am sending different parameters to constructor of XElement by adding new XElement("Temp_F",Value) but i want to add condition that if my value is null or empty then don't create and add XElememt parameter.

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);
                }
            }
Zaid Iqbal
  • 1,662
  • 5
  • 25
  • 45
  • Did u tried this `if (!string.IsNullOrEmpty(medicationEntity.Value)) { new XElement("Temp_F", medicationEntity.Value), }` – Mohit S Jul 10 '14 at 04:56

2 Answers2

1

This should do the trick:

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)
        ));
Darek
  • 4,687
  • 31
  • 47
1

"..i want to add condition that if my value is null or empty then don't create and add XElememt parameter."

So please add the conditional checks, any problem with this approach? :

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);
    }
}
har07
  • 88,338
  • 12
  • 84
  • 137