There are many ways to write the data to a file. The following example shows reading the data from xml file and writing to a file. Here the file could be any type, like “.txt” or “.sql” etc.
Input XML is:
<subjects>
<subject id="100">
<name>ASP.NET</name>
</subject>
<subject id="200">
<name>C#</name>
</subject>
</subjects>
Sample Code:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("input.xml");
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("subject");
StringBuilder text = new StringBuilder();
string strName = string.Empty;
foreach (XmlNode node in xmlnode)
{
text.AppendLine("Subject Id: " + node.Attributes["id"].Value);
text.AppendLine("Subject Name: " + node["name"].InnerText);
}
using (StreamWriter file =
new StreamWriter(@"output.txt"))
{
file.Write(text.ToString());
}
No comments:
Post a Comment