C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

C# code for reading and writing XML file in simple way


Posted Date: 15 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Anand_lvMember Level: Silver    
Rating: 1 out of 5Points: 10



This is a simple way to read and write XML file using C#.



XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"D:\anandlv\xmlReading\WindowsApplication3\XMLFile1.xml");
XmlTextReader xReader = new XmlTextReader();
xDoc.SelectNodes(@"D:\anandlv\WindowsAppliction3\WindowsApplication3\XMLFile1.xml");
XmlNode node;
string sVar = node.ChildNodes[0].Attributes["num"].Value;
XmlNodeList xN1 = xDoc.GetElementsByTagName("val1");
XmlNodeList xN2 = xDoc.GetElementsByTagName("val2");
MessageBox.Show(xN2.Count.ToString());
foreach (XmlNode node in xN1)
{
MessageBox.Show(node.InnerText);
foreach (XmlAttribute attr in node.Attributes)
{
MessageBox.Show(attr.Name + "," + attr.Value);
}
}

THESE ARE THE CODE TO WRITE XML FILE

XmlTextWriter xWriter = new XmlTextWriter(@"D:\anandlv\xmlReading\WindowsApplication3\XMLFile1.xml", null);
xWriter.WriteStartDocument();//starting document

xWriter.WriteStartElement("values");//starting parent node
xWriter.WriteStartElement("values1");//1st child node

xWriter.WriteAttributeString("id", "1000");//attributes of child node
xWriter.WriteAttributeString("name", "ABC");
xWriter.WriteString("ISO Company");//innertext
xWriter.WriteEndElement();
xWriter.WriteStartElement("num");//2nd child node
xWriter.WriteAttributeString("more", "100");
xWriter.WriteAttributeString("less", "10");
xWriter.WriteString("numeric");
xWriter.WriteEndElement();
xWriter.WriteStartElement("r", "runnnig", "");//3rd child node
xWriter.WriteAttributeString("fast", "1000km");
xWriter.WriteAttributeString("slow", "100km");
xWriter.WriteString("killometers");
xWriter.WriteEndElement() ;
xWriter.WriteStartElement("character");
xWriter.WriteAttributeString("char", "a");
xWriter.WriteAttributeString("another", "b");
xWriter.WriteEndElement();

xWriter.WriteEndElement();
xWriter.WriteEndDocument();
xWriter.Close();





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
XML READING AND WRITING  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: using security.principle and environment namespace
Previous Resource: Get values from a database to datagrid
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use