开发者

How do I cast a XElement as string in c#?

Here is my code:

SqlConnection conn4 = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=test_BdbCSSQL01;Persist Security Info=False;Integrated Security=SSPI;");
conn4.Open();

string sql = "SELECT * FROM ERROROfSIDESStagingOUT";
SqlDataAdapter da = new SqlDataAdapter(sql, conn4);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);

XDocument doc = XDocument.Load("XmlString.xml");
XNamespace ns = "https://uidataexchange.org/schemas";

var node = doc.Descendants(ns + "EmployerTPASeparationResponse");
var node2 = node.ElementAt(i);

        foreach (var param in node2.Elements())
        {

            try
            {
                if (dr[par开发者_Python百科am.Name.LocalName].ToString() == "PriorIncidentOccurrence")
                {
                    var PriorIncidentDescendants = param.Descendants(ns + "PriorIncidentOccurrence");
                    dr["PriorIncidentID"] = PriorIncidentDescendants.ElementAt(0).Value;                                        
                }

                if (dr.Table.Columns.Contains(param.Name.LocalName))
                {
                    dr[param.Name.LocalName] = param.Value;
                }
            }
            catch (Exception ee)
            {
                //TODO: SendMail
                string asdf = ee.ToString();
            }
        }
        SqlCommandBuilder sb = new SqlCommandBuilder(da);
        da.Update(dt);

if (conn4 != null)
{
    conn4.Close();
}

I am trying to cast dr[param.Name.LocalName] as type string. Both of the following do not work.

(string)dr[param.Name.LocalName]
dr[param.Name.LocalName].ToString()


It is my guess, that you are calling dr[param.Name.LocalName] before you have assigned a value to that key in your first if statement.

You should probably state the error you recieve. I'm guessing it has nothing to do with casting...

You could try to check the value of param.Name.LocalName in the debugger (or print it with Debug.WriteLine) and make sure that the values are all valid column names in your DataRow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜