0
這裏是我的代碼:如何在C#中將字符串轉換爲XElement?
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[param.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();
}
我想投dr[param.Name.LocalName]
爲String類型。以下兩種都不起作用。
(string)dr[param.Name.LocalName]
dr[param.Name.LocalName].ToString()
但是你需要什麼?這種XElement中的嵌套文本節點還是序列化XElement以便獲得XML片段? – 2011-05-13 13:51:59
@Matias:我正在試圖將[drram [Name.LocalName])轉換爲字符串。 – Zach 2011-05-13 13:54:14
我重複同樣的問題,因爲它似乎你不是很確定你想要這個「演員」的結果..? – 2011-05-13 13:56:34