<?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,现在我必须添加属性&#34; assignee = abc&#34; in&#34; userTask&#34;标记并将其再次保存到数据库 我正在尝试这个代码,它没有给出任何错误,但是没有添加属性。
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
答案 0 :(得分:0)
您的代码看起来是正确的。检查你的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");