Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create "User" node to this xml file:

XML
<User PIN ="5111">
    <ID>123450</ID>
    <tcounter>1</tcounter>
    <Name>Adam Lin</Name>
    <CheckingACCNum>8565478</CheckingACCNum>
    <CheckingACCBalance>4500</CheckingACCBalance>
    <SavingACCNum>741844</SavingACCNum>
    <SavingACCBalance>0</SavingACCBalance>
    <Address>1120 Virginia Ave., Nashville, TN 37210</Address>
    <Phone>(615) 963-4444</Phone>
    <current>1</current>
    <t1> </t1>
    <t2> </t2>
    <t3> </t3>
    <t4> </t4>
    <t5> </t5>
    <t6> </t6>
    <t7> </t7>
    <t8> </t8>
    <t9> </t9>
    <t10> </t10>
    <t11> </t11>
    <t12> </t12>
    <t13> </t13>
    <t14> </t14>
    <t15> </t15>
    <t16> </t16>
    <t17> </t17>
    <t18> </t18>
    <t19> </t19>
    <t20> </t20>
    </User>


What I have tried:

C#
using (XmlTextWriter write = new XmlTextWriter("USERS.xml", Encoding.UTF8))
            {
                write.WriteStartElement("User");
                write.WriteAttributeString("PIN", userPIN.Text);
                write.WriteElementString("ID",usrid.Text);
                write.WriteElementString("Name", usrname.Text);
                write.WriteElementString("CheckingACCNum", usrcheckingaccnum.Text);
                write.WriteElementString("CheckingACCBalance",      usrcheckingaccBalance.Text);
                write.WriteElementString("Name", usrname.Text);
                write.WriteElementString("SavingACCNum", usrsavingaccNum.Text);
                write.WriteElementString("SavingACCBalance", usrcsavingaccBalance.Text);
                write.WriteElementString("Address", useraddress.Text);
                write.WriteElementString("Phone", usrphone.Text);
                write.WriteElementString("tcounter", "1");
                write.WriteElementString("current", "1");
                write.WriteEndElement();
                doc11.Save("USERS.xml");
Posted
Updated 21-Mar-16 20:39pm
v3

Its very simple. Check the following code,
C#
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\users.xml");

DataRow objNewRow = objDataSet.Tables[0].NewRow();
objNewRow["PIN"] = "012345";
objNewRow["ID"] = "98765";
objNewRow["Name"] = "Testing User";
objNewRow["CheckingACCNum"] = "258741369";
objNewRow["CheckingACCBalance"] = "50000";
objNewRow["SavingACCNum"] = "789456123";
objNewRow["SavingACCBalance"] = "45000";
objNewRow["Address"] = "United States";
objNewRow["Phone"] = "1231231230";
objNewRow["tcounter"] = "10";
objNewRow["current"] = "565";

objDataSet.Tables[0].Rows.Add(objNewRow);
objDataSet.Tables[0].AcceptChanges();

objDataSet.Tables[0].WriteXml(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\new_users.xml");

I have used your user.xml file from my desktop folder. You just provide your own xml file path in the following functions.
C#
objDataSet.ReadXml("Your XML file path");
objDataSet.Tables[0].WriteXml("Your XML file path");
 
Share this answer
 
v2
Comments
coding star 22-Mar-16 2:34am    
What if my file is in the same solution folder??
I tried
objDataSet.ReadXml("USERS.xml");
VR Karthikeyan 22-Mar-16 3:00am    
Use like this,
objDataSet.ReadXml("../../users.xml"); to access a file present in your project folder.
I think XMLDocument class will help you more than XMLWriter, it is very easy just create new node and append it to existing xml
see below sample snippet

C#
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlElement foo = doc.CreateElement("foo");
XmlElement bar = doc.CreateElement("bar");
bar.InnerText = "whatever";
foo.AppendChild(bar);
doc.DocumentElement.AppendChild(foo);
doc.Save("file.xml");
 
Share this answer
 
Quote:
I want to create "User" node to this xml file:
The file you show is wrong afainst what you want to do.
In order to add a user to the file, I think you need a root node.
Your file
XML
<user pin="5111">
...
</user>

must look like
XML
<rootnode>
  <user pin="5111">
  ...
  </user>
</rootnode>

in order to add User nodes
XML
<rootnode>
  <user pin="5111">
  ...
  </user>
  <user pin="5112">
  ...
  </user>
</rootnode>

To change an XML file, one need to load the file in memory (with an XMLReader object/class). Add nodes as you want. Save the file when finished. Like shown in Solution 1.
XML Tutorial[^]
 
Share this answer
 
v2

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