<tree id="0">
<item text="Books" id="books" open="1" im0="tombs.gif" im1="tombs.gif" im2="iconSafe.gif" call="1" select="1">
<item text="Mystery &amp; Thrillers" id="mystery" im0="folderClosed.gif" im1="folderOpen.gif" im2="folderClosed.gif">
<item text="Lawrence Block" id="lb" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif">
<item text="All the Flowers Are Dying" id="lb_1" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"/>
<item text="The Burglar on the Prowl" id="lb_2" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"/>
<item text="The Plot Thickens" id="lb_3" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"/>
<item text="Grifter's Game" id="lb_4" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"/>
<item text="The Burglar Who Thought He Was Bogart" id="lb_5" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"/>
</item>
</item>
</item>如果使用XmlDocument,那么text="Lawrence Block" id="lb" im0="book_titel.gif" im1="book.gif" im2="book_titel.gif"这些应该怎么添加进去。

解决方案 »

  1.   

    xmldocument 设置xmlnode的arrtibutes
    public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                        {
                            xe.InnerText = value;
                        }
                        else
                        {
                            xe.SetAttribute(attribute, value);
                        }
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }