Click here to Skip to main content
15,891,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the structure of xml File
XML
<?xml version="1.0" encoding="utf-8" ?>
<users>
   <user id="JCollins">
     <fullname>Jim Collins</fullname>
     <deppath>\\JCollinslt02\c$</deppath>
   </user>
<users>


I want to insert a new user
How can i do that i am confused in the way the userid is inserted

Please Help

Thanks and regards.
suhailnoorpawane
Posted

1 solution

XML
private XmlDocument DataTableToXmlDocument(ref DataTable dt)
<pre>{
  XmlDocument xml= new XmlDocument();
  StringBuilder sb = new StringBuilder();

  sb.Append("<?xml version='1.0' encoding='utf-8' ?>");
  sb.Append("<DocumentElement>");

  foreach (DataRow dr in dt.Rows)
  {
    sb.Append("<" + dt.TableName + ">");

    foreach (DataColumn dc in dt.Columns)
      sb.Append("<" + dc.ColumnName + ">" +
              dr[dc].ToString() +
           "</" + dc.ColumnName + ">");

      sb.Append("</" + dt.TableName + ">");
  }

  sb.Append("</DocumentElement>");
  xml.LoadXml(sb.ToString());

  return xml;
}
 
Share this answer
 
Comments
suhailnoorpawane 7-Dec-11 3:33am    
will this code add the way userid
[no name] 2-Mar-12 4:45am    
string path = Server.MapPath(@"My_XML.xml");
string id;
XmlDocument doc = new XmlDocument();
doc.Load(path);
using (XmlReader reader = XmlReader.Create(path))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{

if (reader.Name == "user")
{
if (reader.HasAttributes == true)
{
id = reader.GetAttribute(0);
ViewState["id"] = id;
}


}


}


}

}
int i = Convert.ToInt32(ViewState["id"].ToString());
i++;
XmlElement element = doc.CreateElement("artist");
element.SetAttribute("id", i.ToString());
element.InnerXml = "<country>" + Txtcountry.Text + "" + "<company>" + Txtcompany.Text + "" + "<price>" + Txtprice.Text + "</price>" + "<year>" + Txtyear.Text + "";
XmlNode root = doc.DocumentElement;
root.InsertAfter(element, root.LastChild);
doc.Save(path);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900