且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在XML文件中插入数据

更新时间:2023-11-07 12:24:16

假设您的根元素employee存在于XML文件中,那么您的代码就会出现问题,因为myxmlnode将如果您的根节点目前没有子元素,则始终指向null.您可能可以像这样编辑分配值的方式.
Assuming your root element employee exists on the XML file, there would be a problem on your code, since myxmlnode will always point to null if your root node does not have child elements at the moment. You can probably edit the way you assign the value like this.
XmlNode myxmlnode = myxmldocument.DocumentElement;


然后,您可以像这样将子节点添加到您的根节点(员工).


And then, you can then add child nodes to your root node(employee) like this.

XmlElement myXmlElement = myxmldocument.CreateElement("Name");
myXmlElement.InnerText = Server.HtmlEncode(TextBox1.Text);
myxmlnode.AppendChild(myXmlElement);


这是否正常工作,我添加了像这样的子对象

Is this working correctly i add childs like this

XmlText text = myxmldocument.CreateTextNode(Server.HtmlEncode(TextBox1.Text)); myxmldocument.DocumentElement.AppendChild(myXmlElement);
myxmldocument.DocumentElement.LastChild.AppendChild(text);



但是o/p显示正确的格式.....



But o/p shows correct format.....