[英]Adding attribute in the node of xml using c#
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
<process id="sid-C3803939-0872-457F-8336-EAE484DC4A04" name="Customer" processType="None" isClosed="false" isExecutable="false">
<userTask id="Task_1fxai2y" name="ut124" />
</process>
我正在從數據庫中檢索此XML,現在我必須在“ userTask”標簽中添加屬性“ assignee = abc”並將其再次保存到數據庫中,我正在嘗試此代碼,它沒有給出任何錯誤,但是該屬性沒有添加。
string a;
SqlCommand cmd2 = new SqlCommand("select * from usertask1 where DIAGRAMID
= " + idnum + "", con);
con.Open();
SqlDataReader rdr2 = cmd2.ExecuteReader();
if (rdr2.HasRows)
{
while (rdr2.Read())
{
string a = rdr2["XMLFILE"].ToString();// variable 'a' now has the xml
XmlDocument xd = new XmlDocument();
xd.LoadXml(a);
XmlNodeList list = xd.GetElementsByTagName("userTask");
XmlAttribute XA = xd.CreateAttribute("ASSIGNEE");
XA.Value = "abc";
list[0].Attributes.Append(XA);
}
}
//code to insert the xml again back to DB
您的代碼看起來正確。 檢查您的xml結構或也嘗試這種方法
XmlDocument xd = new XmlDocument();
xd.LoadXml("<root>" +
"<userTask></userTask>" +
"<userTask></userTask>" +
"</root>");
XmlNodeList list = xd.GetElementsByTagName("userTask");
XmlElement el = (XmlElement)list[0];
el.SetAttribute("ASSIGNEE", "abc");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.