473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Xml Read Single Line at a time using C#

Hi ,
I have to extract a xml file line by line. Here is my xml
Step1: <category name= "name" guid="{guid}"state="enabled">
Step2: <rules>
Step3: <rule name="rule name" guid="{guid}"
Step4: <\rule>
Step5: <\rules>
Step6: <\category>
So I have to extract, only one step at a single time;
I have written a sample code but it was not working properly. So can
any one provide the sample code will be better information for me?

Here is my sample code.
XmlTextReader tr = new XmlTextReader(txtFirstMonitorPack.Text);

while(tr.Read())
{
if(tr.NodeType == XmlNodeType.Element )
{
//tr.ReadString();
System.Console.WriteLine(tr.Value);
}

}

I am very poor in expressing the problem. so for that I am giving my
required out here
Just like this :
Writeline("<category name= "name" guid="{guid}"state="enabled">");
Means I want to read only first line at a single time.

Regards,
Rs.

Feb 27 '07 #1
2 8069
sk**************@gmail.com wrote:
Hi ,
I have to extract a xml file line by line. Here is my xml
Step1: <category name= "name" guid="{guid}"state="enabled">
Step2: <rules>
Step3: <rule name="rule name" guid="{guid}"
Step4: <\rule>
Step5: <\rules>
Step6: <\category>
So I have to extract, only one step at a single time;
I have written a sample code but it was not working properly. So can
any one provide the sample code will be better information for me?

Here is my sample code.
XmlTextReader tr = new XmlTextReader(txtFirstMonitorPack.Text);

while(tr.Read())
{
if(tr.NodeType == XmlNodeType.Element )
{
//tr.ReadString();
System.Console.WriteLine(tr.Value);
}

}

I am very poor in expressing the problem. so for that I am giving my
required out here
Just like this :
Writeline("<category name= "name" guid="{guid}"state="enabled">");
Means I want to read only first line at a single time.
While the file is XML, you are wanting to read it as you would a standard
text file. As such, use TextReader derived class rather than XmlTextReader.
Ex:

using (StreamReader sr = new StreamReader(txtFirstMonitorPack.Text))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
--
Tom Porterfield

Feb 27 '07 #2
<sk**************@gmail.comwrote:
I have to extract a xml file line by line. Here is my xml
Step1: <category name= "name" guid="{guid}"state="enabled">
Step2: <rules>
Step3: <rule name="rule name" guid="{guid}"
Step4: <\rule>
Step5: <\rules>
Step6: <\category>
It may be helpful at some stage to see the real file - that's not even
slightly valid XML.
So I have to extract, only one step at a single time;
I have written a sample code but it was not working properly. So can
any one provide the sample code will be better information for me?

Here is my sample code.
XmlTextReader tr = new XmlTextReader(txtFirstMonitorPack.Text);
You can't use XmlTextReader to read line by line - it reads node by
node, and sometimes you can have more than one node in a single line,
or one node that spans multiple lines.

To read line by line, use a TextReader such as StreamReader.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
7
by: Mike | last post by:
Hi, I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code: do { readBytes = base.GetStream().Read(received, 0,...
3
by: - Steve - | last post by:
Okay I call Console.Read() twice. However when I come up on my second Console.Read() ASCII character 10 is already on the console (that's a line feed). How do I clear out that line feed before...
1
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
11
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.