Em đang thực hành thao tác trên cơ sở dữ liệu XML. Em có thắc mắc về vấn đề nay:
em có 1 đoạn code XML như thế.
Code:
<account>
<abc ID="kaka" Pass="123"/>
<asd ID="kuti" Pass="123"/>
</account>
em muốn thêm 1 node con cho account. Nhưng em code ở dưới đây.Thì xuất hiện lỗi "The node to be inserted is from a different document context."
ở trên em có khai báo
Code:
public void load(string path)
{
XmlDocument xmlreader = new XmlDocument();
xmlreader.Load(path);
XmlNodeList listnode = xmlreader.ChildNodes;
for (int i = 0; i < listnode.Count; i++)
{
if (listnode[i].Name.Equals("account"))
{
root = listnode[i];
}
}
}
public void newnode(string user, string loginname, string pass)
{
XmlDocument doc = new XmlDocument();
XmlNode node = doc.CreateElement(user);
XmlAttribute attlg = doc.CreateAttribute("LoginName");
attlg.Value =loginname;
node.Attributes.Append(attlg);
XmlAttribute attpw = doc.CreateAttribute("Password");
attpw.Value=pass;
node.Attributes.Append(attpw);
root.AppendChild(node);//---->lỗi
MessageBox.Show("ok");
}