there is some XML syntex issue in your file. please check the file as It might be missing the root element that every xml should contain.
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
// update MainCategory
nodeList[0].ChildNodes[0].InnerText = "ASP.NET";
// update Description
nodeList[0].ChildNodes[1].InnerText = "This is now my ASP.NET list.";
// update Active
nodeList[0].ChildNodes[2].InnerText = "false";
Your code is perfect. The one and only problem is that the xdoc.DocumentElement is NULL. so you have to feeds it giving some element. Refer my code below for the same.
exbond
Member
58 Points
144 Posts
how to write in xml?
Apr 11, 2012 09:09 AM|LINK
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" CssClass="textbox" ></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="post" CssClass="button" onclick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(Server.MapPath(@"chat.xml")); //error root element not found XmlElement xe = xdoc.CreateElement("chat"); XmlElement xchatpost = xdoc.CreateElement("chatpost"); xchatpost.InnerText = DateTime.Now.ToString(); xe.AppendChild(xchatpost); XmlElement xpost = xdoc.CreateElement("post"); xpost.InnerText = TextBox1.Text; xe.AppendChild(xpost); xdoc.DocumentElement.AppendChild(xe); xdoc.Save(Server.MapPath("chat.xml")); Response.Write("posted..."); }this code gives an error : root element not found?
chikkanti
Member
270 Points
80 Posts
Re: how to write in xml?
Apr 11, 2012 09:15 AM|LINK
//Start writer
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/XMLFiles/" + xmlfile_name + ".xml"), System.Text.Encoding.UTF8);
//Start XM DOcument
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
//ROOT Element
writer.WriteStartElement("Books");
//call create nodes method
createNode(book_name, book_price, author1_fname, author1_lname, author2_fname, author2_lname, author1_country, author1_state, author2_country, author2_state, writer);
writer.WriteEndElement();
//End XML Document
writer.WriteEndDocument();
//Close writer
writer.Close();
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: how to write in xml?
Apr 11, 2012 09:16 AM|LINK
there is some XML syntex issue in your file. please check the file as It might be missing the root element that every xml should contain.
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
ramiramilu
All-Star
95295 Points
14072 Posts
Re: how to write in xml?
Apr 11, 2012 09:18 AM|LINK
check out here on how to create root node for XmlDocument - http://www.devx.com/tips/Tip/21168
Thanks,
JumpStart
ItsSunny
Contributor
2163 Points
676 Posts
Re: how to write in xml?
Apr 11, 2012 09:20 AM|LINK
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("categories.xml"));
XmlNodeList nodeList = xmlDoc.SelectNodes("/CategoryList/Category[@ID='01']");
// update MainCategory
nodeList[0].ChildNodes[0].InnerText = "ASP.NET";
// update Description
nodeList[0].ChildNodes[1].InnerText = "This is now my ASP.NET list.";
// update Active
nodeList[0].ChildNodes[2].InnerText = "false";
xmlDoc.Save(Server.MapPath("categories.xml"))
Arun Sunny.
evello880
Member
226 Points
75 Posts
Re: how to write in xml?
Apr 11, 2012 09:24 AM|LINK
http://forums.asp.net/t/1469398.aspx/1
kavita_khand...
Star
9767 Points
1930 Posts
Re: how to write in xml?
Apr 11, 2012 09:59 AM|LINK
@exbond
Your code is perfect. The one and only problem is that the xdoc.DocumentElement is NULL. so you have to feeds it giving some element. Refer my code below for the same.
XmlDocument xdoc = new XmlDocument(); //xdoc.Load(Server.MapPath(@"chat.xml")); //error root element not found string myRoot = "<chat></chat>"; xdoc.LoadXml(myRoot); XmlElement xe = xdoc.CreateElement("chat"); XmlElement xchatpost = xdoc.CreateElement("chatpost"); xchatpost.InnerText = DateTime.Now.ToString(); xe.AppendChild(xchatpost); XmlElement xpost = xdoc.CreateElement("post"); xpost.InnerText = "temp text"; //TextBox1.Text; xe.AppendChild(xpost); xdoc.DocumentElement.AppendChild(xe); xdoc.Save(Server.MapPath("chat.xml")); Response.Write("posted...");HTH!
I would love to change the world, but they wont give me the source code.
exbond
Member
58 Points
144 Posts
Re: how to write in xml?
Apr 11, 2012 10:23 AM|LINK
my code is perfect it only missing the root element!!!!
if i write the root node it will work
thank u @kavita