Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Current Output From the XML file When i run my code - fanemlname0772569984ac@gs.com7/17/2013 9:04:48 AM
Required Output From the XML file When i run my code - fname,lname,0772569984,ac@gs.com,7/17/2013, 9:04:48 AM

Help me out guys thanks!!!!
This is my Code
XML File i used is below after the code
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;


namespace ReadXMLfromFile
{

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

                        break;
                    case XmlNodeType.Text: 
                        Console.WriteLine(reader.Value);
                        break;
                }
            }
            Console.ReadLine();
            string onlyContent = string.Empty;

            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(@"C:\Users\afshandc\Desktop\ConsoleApplication1\ConsoleApplication1\Visitors (2).xml");

            var visitors = xdoc.SelectNodes("Visitors/Visitor");
            for (int i = 0; i < visitors.Count; i++)
            {

                onlyContent += string.Format("\n", i);

                foreach (XmlNode node in visitors[i].ChildNodes)
                    onlyContent += string.Format("{0}", node.InnerText);
            }

            File.WriteAllText(@"C:\Users\afshandc\Desktop\ConsoleApplication1\ConsoleApplication1\Written XML.txt", onlyContent);
        }
    }

}



XML
<Visitors>
  <Visitor>
    <FirstName>fanem</FirstName>
    <LastName>lname</LastName>
    <ContactNo>0772569984</ContactNo>
    <Email>ac@gs.com</Email>
    <Date>7/17/2013 9:04:48 AM</Date>
  </Visitor>
  <Visitor>
    <FirstName>fname22</FirstName>
    <LastName>Lname234</LastName>
    <ContactNo>1234567890</ContactNo>
    <Email>visitorgmail.com</Email>
    <Date>7/17/2013 9:11:02 AM</Date>
  </Visitor>
Posted
Updated 17-Jul-13 20:27pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Jul-13 2:35am    
You don't require anything certain. That TXT file can be anything. After all, and XML file is already a text file. You need to describe the output file, how you map your XML on it. The question does not really make sense unless you do it.
—SA
midnight_ 18-Jul-13 2:45am    
Sergey, seems to me that you are a critic, and unless the question doesn't makes sense to you, it makes sense to the questioner. So you dont have to downvote answers/solutions or blame people for asking.
This is Q&A and not Good||Bad.
Sergey Alexandrovich Kryukov 18-Jul-13 3:00am    
Who blames anyone for asking? But the question should be improved, to help inquirer to get help, otherwise all this activity and answers would be only a waste of time.
—SA
midnight_ 18-Jul-13 3:29am    
What should be improved, when the inquirer is satified with the answer and mark it as solution? The problem got fixed, activity closed.

There's no need to reply your opinion about how bad and how many waste of time this is.
If it makes no sense to you, then you shouldn't participate.
Sergey Alexandrovich Kryukov 18-Jul-13 10:31am    
Please let me decide by myself where to participate and where not. I have very certain opinion about it, in many cases. As to the marking the solution: it should not close the activity. No way. Adding some limitations here would be really harmful. The solution is not the end. It could be totally wrong, and it could be perfect, yet someone else can add something very useful, in yet another solution. Did I get you right though?
—SA

var visitors = xdoc.SelectNodes("Visitors/Visitor");
for (int i = 0; i < visitors.Count; i++)
{
foreach (XmlNode node in visitors[i].ChildNodes)
onlyContent += string.Format("{0},", node.InnerText);
onlyContent = onlyContent.TrimEnd(',');
onlyContent += string.Format("{0}", Environment.NewLine);
}

This also works for your requirement.
Just thought of posting.
Cheers
B.S
 
Share this answer
 
Comments
Hawkeye101 18-Jul-13 4:08am    
Thanks but i already got a solution! Appreciate your help! :)
afshandc you reposted

C#
namespace ReadXMLfromFile
{
    class Class1
    {
        static void Main(string[] args)
        {
            string onlyContent = string.Empty;

            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(@"C:\Users\afshandc\Desktop\ConsoleApplication1\ConsoleApplication1\Visitors (2).xml");

            var visitors = xdoc.SelectNodes("Visitors/Visitor");
            for (int i = 0; i < visitors.Count; i++)
            {
                onlyContent += string.Format("\n", i);

                foreach (XmlNode node in visitors[i].ChildNodes)
                    onlyContent += string.Format("{0},", node.InnerText);
            }

            File.WriteAllText(@"C:\Users\afshandc\Desktop\ConsoleApplication1\ConsoleApplication1\Written XML.txt", onlyContent);
        }
    }

}
 
Share this answer
 
Comments
Hawkeye101 18-Jul-13 2:26am    
Yup Needed to get it perfectly as i wanted :( Thats the reason! Any help??
midnight_ 18-Jul-13 2:30am    
Just paste the code above in your solution. Should work as you expected
Hawkeye101 18-Jul-13 2:36am    
Thanks a lot!!!! :) :)
midnight_ 18-Jul-13 2:39am    
your welcome - one more thing:

the line: onlyContent += string.Format("\n", i);
should look like this (if you wish a counting number at the beginning)

onlyContent += string.format("{0}:", i);
Hawkeye101 18-Jul-13 2:41am    
Alright! :)

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