Search code examples
c#.netxmlfunctional-programmingxelement

Add Parameter to XElment on Runtime C#


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

Solution

  • "..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);
        }
    }