Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to read a xml document in c# and drop the data to notepad
This is the XML File(I DONT WANT TO WRITE THE TAGS TO NOTEPAD,I ONLY THE INFO)
Please Help!

<Visitors><br />
  <Visitor><br />
    <FirstName>Test1</FirstName><br />
    <LastName>Test12</LastName><br />
    <ContactNo>1234567890</ContactNo><br />
    <Email>test@test.com</Email><br />
    <Date>7/17/2013 9:04:48 AM</Date><br />
  </Visitor><br />
</Visitors>
Posted
Updated 16-Jul-13 21:06pm
v2

Hi afshandc - this should to the trick:

C#
string onlyContent = string.Empty;

XmlDocument xdoc = new XmlDocument();
xdoc.Load(pathToXml);

var visitors = xdoc.SelectNodes("Visitors/Visitor");
for (int i = 0; i < visitors.Count; i++)
{
   //headlining the new entry
   onlyContent += string.Format("----VISITOR {0}------\r\n",i);

   //read all nodes innertext 
   foreach (XmlNode node in visitors[i].ChildNodes)
     onlyContent += string.Format("{0}\r\n", node.InnerText);
}

File.WriteAllText(yourOutputfile, onlyContent);
 
Share this answer
 
v2
Comments
Hawkeye101 17-Jul-13 5:24am    
Still ddnt work!Throws an XML exception unhanded error!!
H.Brydon 17-Jul-13 12:49pm    
I am not a C# person but this solution seems to be the closest to the answer (+4). If you don't want the xml tags, you want to retrieve the innerHtml or innerText from either the "Visitors" or "Visitor" nodes. This can be a one-liner. You don't need to do it in a loop.
midnight_ 18-Jul-13 1:18am    
Brydon is right - this could be a one-liner. However if you use just the innerText of the whole document, you would get one string with no linebreak or spaces between the elements.

@afshandc: do you got a error message?
Hawkeye101 18-Jul-13 1:44am    
@midnight_ Thanks got it done! :)
C#
private static DataSet ReadXMLFile(string filePath)
       {
           try
           {
               DataSet ds = new DataSet();
               if (!string.IsNullOrEmpty(filePath))
               {
                   ds.ReadXml(filePath);
               }
               return ds;
           }
           catch (Exception)
           {

               throw;
           }
       }


data will be save in dataset know you can write data as you want.

helping links for writing on file

Write to a Text File

TextWriter
 
Share this answer
 
quick 'n dirty:

C#
var xmlContent = File.ReadAllText(pathToXml);
File.WriteAllText(pathToTextfile, xmlContent);
 
Share this answer
 
Comments
Hawkeye101 17-Jul-13 2:35am    
This is My code but it isnt writing the info to the text document!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;



namespace ReadXMLfromFile
{
///
/// Summary description for Class1.
///

class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("Visitors.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.WriteLine (reader.Value);

break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;

}
}
Console.ReadLine();
var xmlContent = File.ReadAllText(@"C:\Users\Geeth\Desktop\First Project\ConsoleApplication1\ConsoleApplication1\Visitors.xml");
File.WriteAllText(@"C:\Users\Geeth\Desktop\First Project\ConsoleApplication1\ConsoleApplication1\Written XML.txt", xmlContent); }
}
}
Try this link for reading the XML. Use StreamWriter to write the output to Text File.

http://support.microsoft.com/kb/307548[^]
 
Share this answer
 

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