c# - ASP.NET: Adding xml node to first position of file -
i´d have news feed on web, create admin page , simple form add news node xml file. add node last position, need on first position.
my code (in c#):
xmldocument xmlfile = new xmldocument(); xmlfile.load(server.mappath("/news.xml")); xmlelement news = xmlfile.createelement("news"); xmlelement title = xmlfile.createelement("title"); xmlelement content = xmlfile.createelement("content"); xmltext titletext = xmlfile.createtextnode(textbox_title.text); xmltext contenttext = xmlfile.createtextnode(textbox_content.text); title.appendchild(titletext); content.appendchild(contenttext); news.appendchild(title); news.appendchild(content); xmlnode news = xmlfile.getelementsbytagname("news")[0]; //xmlfile.documentelement.appendchild(news); xmlfile.save(server.mappath("/news.xml"));
my xml file:
<thenews> <news> <title> example title </title> <content> example content. </content> </news> </thenews>
code reading xml:
`<asp:listview id="newslist" runat="server" datasourceid="xmldatasource"> <itemtemplate> <h2><%# xpath("title") %></h2> <p> <%# xpath("content") %> <hr /> </p> </itemtemplate> </asp:listview> <asp:xmldatasource id="xmldatasource" runat="server" datafile="~/news.xml"></asp:xmldatasource>`
try prependchild
instead of appendchild
:
xmlfile.documentelement.prependchild(news);
Comments
Post a Comment