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 » Articles » .NET Framework »

Create XML file using C# .NET (with indentation)


Posted Date: 04 Jun 2004    Resource Type: Articles    Category: .NET Framework
Author: ShajanMember Level: Silver    
Rating: 1 out of 5Points: 4




public void CreateXMLFile(string FileName)
{
DateTime erDate=DateTime.Now;
string strDate=erDate.Day.ToString() + "/" + erDate.Month.ToString() + "/" + erDate.Year.ToString();
string strTime=erDate.Hour.ToString() + ":" + erDate.Minute.ToString() + ":" + erDate.Second.ToString() + ":" + erDate.Millisecond.ToString();
string path;
path=System.AppDomain.CurrentDomain.BaseDirectory.ToString() + FileName + ".xml";
System.Xml.XmlDocument doc =new System.Xml.XmlDocument();
System.IO.FileInfo fileName=new System.IO.FileInfo(path);
try
{
if (!fileName.Exists)
{
StreamWriter sw = new StreamWriter(path,true);
sw.WriteLine("<BaseElement>");
sw.WriteLine("</BaseElement>");
sw.Flush();
sw.Close();
}
doc.Load(path);
XmlElement newElem;
XmlElement newChElem;
XmlElement newSuperChElem;
XmlNode XNode;

try
{
XNode= doc.SelectSingleNode("//ElementName[@AttributeName='AttributeValue']");
XNode.Attributes.Item(0).Value="AttributeValue";
}
catch(Exception ex)
{
}
newElem= doc.CreateElement("ElementName");
doc.DocumentElement.AppendChild(newElem);
newElem.SetAttribute("AttributeName","AttributeValue");

newChElem = doc.CreateElement("ElementName");
newChElem.InnerText = Value;
newElem.AppendChild(newChElem);

newChElem = doc.CreateElement("Date");
newChElem.InnerText = strDate;
newElem.AppendChild(newChElem);

newChElem = doc.CreateElement("Time");
newChElem.InnerText = strTime;
newElem.AppendChild(newChElem);

XmlTextWriter writer = new XmlTextWriter(path,System.Text.Encoding.ASCII);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
catch(Exception ex)
{
//Your Catch block here...
}




Responses

Author: critic    15 Jun 2004Member Level: Bronze   Points : 0
What you mean by
"Create an XML file using C#
Explains how to create XML elements, Nodes and setting attributes and updating earlier attributes..."


An artile should have a meaningful description. Take few minutes to think and submit a proper sub title and description.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

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: Assigning One Array Reference Variable To Another.
Previous Resource: Parsing of XML file using C# .NET
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use