How to check if an XML node is null?

1

I have a WS that has a method in which a list of nodes with some data is inserted. I get these data as XmlDocument , I run the Parameters node by reading the contents inside it, after that I get the data and fill in the parameters of my Procedure to insert into the base. Home But there is a possibility that the data sent via WS is null, the node is not created, and when I try to read the error node. How can I check if the node exists and not enter NULL in the Procedure parameter?

I'm using the code that I'm using to validate this, but it returns error

cmdMailingRetorno.Parameters.AddWithValue(
        "@DataHoraFim",
        Telefones[i]["DataHoraFim"].HasAttributes ?
            (DateTime?)null :
            Convert.ToDateTime(Telefones[i]["DataHoraFim"].InnerText)
);
    
asked by anonymous 09.02.2018 / 13:52

1 answer

2

Your validation is inverse if Telefones[i]["DataHoraFim"].HasAttributes is true, you are assigning (DateTime?)null to your parameter value. You can also check if the node exists also before checking that it has attributes, which also does not make much sense since you are hoping to retrieve the value written inside the node.

Then you need to check if the node exists and whether the InnerText is null or has some value.

cmdMailingRetorno.Parameters.AddWithValue(
      "@DataHoraFim",
      (Telefones[i]["DataHoraFim"] == null || !String.IsNullOrEmpty(["DataHoraFim"].InnerText))
      ? null 
      : Convert.ToDateTime(Telefones[i]["DataHoraFim"].InnerText)
);
    
09.02.2018 / 15:42