hello all,

I want to add user data in xmlDocument which has following format:

<proposalRequests>
       <proposal>
              <name>abc</name>
              <contactNo> 998989898 </contactNo>
              <city> ppppp </city>
              <date> 16/07/2009 </date>
       </proposal>
<proposal>
            .....
             .....
</proposal>
</proposalRequests>

i want to append a new <proposal> code for each new user to the XML file.

How do i add nodes and append those in XML file.

Please HELP !!!!!!!!!!!!!!!!

Recommended Answers

All 13 Replies

yash777,
Show us your code where you got a problem. Use classes from System.XML namespace or Use DataSet class.

yes i did used system.xml and XElement following is my code to add XElement in XML File:

XElement xml = new XElement("Proposal", new XElement("Name", txtname.Text),
new XElement("OrgName", txtorgname.Text),new XElement("ContactNo",txtcontactno.Text),
new XElement("ProjectDesc", txtdesc.Text), new XElement("Country", txtcountry.Text),
new XElement("PropExpectedDate", txtdate.Text));

xml.Save(Server.MapPath("~/data/proposals/proposal.xml"));

i know that the main element of <ProposalRequests> is missing.

Can you tell me how to do it using XMLDocument. or is it possible to create and attach the same XElement to XML file for each new entry

Hi you can use the following code..........for creating xml document with your nodes....

XmlDocument xmlDoc = new XmlDocument();
           
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

        XmlElement rootNode = xmlDoc.CreateElement("proposalRequests");
        xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
        xmlDoc.AppendChild(rootNode);
       
        XmlElement parentNode = xmlDoc.CreateElement("proposal");

        
        parentNode.SetAttribute("ID", "01");

        xmlDoc.DocumentElement.PrependChild(parentNode);

      
        XmlElement mainNode = xmlDoc.CreateElement("name");
        XmlElement descNode = xmlDoc.CreateElement("contactNo");
        XmlElement activeNode = xmlDoc.CreateElement("city");

        
        XmlText categoryText = xmlDoc.CreateTextNode("XML");
        XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles.");
        XmlText activeText = xmlDoc.CreateTextNode("true");

       
        parentNode.AppendChild(mainNode);
        parentNode.AppendChild(descNode);
        parentNode.AppendChild(activeNode);
        mainNode.AppendChild(categoryText);
        descNode.AppendChild(descText);
        activeNode.AppendChild(activeText);

      
        xmlDoc.Save(Server.MapPath("categories.xml"));

        Response.Write("XML file created");

Best Regards,
Kameswari

Thank you , as of now i m been able to create a new xml file for each user. Still can't able to append these details in one XML file.

But i will do it by studing ur replies.

Thankx for ur reply.
Be in touch......(help to sort out validation problems)

Thank you , as of now i m been able to create a new xml file for each user. Still can't able to append these details in one XML file.

But i will do it by studing ur replies.

Thankx for ur reply.
Be in touch......(help to sort out validation problems)

Hi Yaash..
try this.......

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("tree.xml"));

XmlNodeList roots = doc.SelectNodes("/Page_Details");

/*
If file not exit then add root node to that xml file
TreeNode root = new TreeNode("root");
this.TreeView1.Nodes.Add(root);
*/

root.Attributes.Append(xmlDoc.CreateAttribute("Page")).InnerText = "HomePage.aspx";
root.Attributes.Append(xmlDoc.CreateAttribute("Time")).InnerText = "10:30:20:32636";

root.Attributes.Append(xmlDoc.CreateAttribute("Type")).InnerText = "S";

root.Attributes.Append(xmlDoc.CreateAttribute("ID")).InnerText
= "1";

root.Attributes.Append(xmlDoc.CreateAttribute("Category_ID")).InnerText = "41";

hello kameshwari,

i m adding textbox value to xmlElement . so please tell me how to do that with ur code

hello kameshwari,

i m adding textbox value to xmlElement . so please tell me how to do that with ur code

Hi Yash,
You can replace the innertext field with your textbox text

root.Attributes.Append(xmlDoc.CreateAttribute("Page")).InnerText =  textBox1.Text;  //let us assume this is ur first textbox name
root.Attributes.Append(xmlDoc.CreateAttribute("Time")).InnerText = textBox2.Text;

Try this...

Best Regards,
Phani

and i want to change the node value not it's attributes.
eg.
<name>yash</name>
<name>Kameshari</name> it should appear like this.....

Ok i did it by using inner Text property.
Now will go to append the xml.
And let you know what happen.............

kameswari,
Use code tags. Source code must be surrounded with code tags.
For example,

[CODE=ASP.NET] ... statements..

[/code]

Kameshwary in ur code

after loading xml file what does . . .

XmlNodeList roots = doc.SelectNodes("/Page_Details");

means.......

Hey here i have updated the code again... suerly it will help u ... try and let me know..

XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(MapPath("~/App_Data/XMLFile.xml"));
        XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();
        string id = TextBox1.Text;
        string name = TextBox2.Text;
        string pwd = TextBox3.Text;
        docFrag.InnerXml =
"<user><id>" + id + "</id><name>" + name + "</name><password>" + pwd + "</password></user>";
        XmlNode childNode = xmlDoc.DocumentElement;
        childNode.InsertAfter(docFrag, childNode.LastChild);
        xmlDoc.Save(MapPath("~/App_Data/XMLFile.xml"));

        Response.Write("UPDATED");

Kameshwary THANKS A LOT , it is working as i want.

Nice code block, i will study it so that i don't have to ask again about XmlDocument.....

Thank you u solved my 2 problems in 2 days.
Added it ur reputation

Keep in touch......


Hey here i have updated the code again... suerly it will help u ... try and let me know..


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(MapPath("~/App_Data/XMLFile.xml"));
XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();
string id = TextBox1.Text;
string name = TextBox2.Text;
string pwd = TextBox3.Text;
docFrag.InnerXml =
"<user><id>" + id + "</id><name>" + name + "</name><password>" + pwd + "</password></user>";
XmlNode childNode = xmlDoc.DocumentElement;
childNode.InsertAfter(docFrag, childNode.LastChild);
xmlDoc.Save(MapPath("~/App_Data/XMLFile.xml"));

Response.Write("UPDATED");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.