473,783 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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}"st ate="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(t xtFirstMonitorP ack.Text);

while(tr.Read() )
{
if(tr.NodeType == XmlNodeType.Ele ment )
{
//tr.ReadString() ;
System.Console. WriteLine(tr.Va lue);
}

}

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

Regards,
Rs.

Feb 27 '07 #1
2 8141
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}"st ate="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(t xtFirstMonitorP ack.Text);

while(tr.Read() )
{
if(tr.NodeType == XmlNodeType.Ele ment )
{
//tr.ReadString() ;
System.Console. WriteLine(tr.Va lue);
}

}

I am very poor in expressing the problem. so for that I am giving my
required out here
Just like this :
Writeline("<cat egory name= "name" guid="{guid}"st ate="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(tx tFirstMonitorPa ck.Text))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLi ne(line);
}
}
--
Tom Porterfield

Feb 27 '07 #2
<sk************ **@gmail.comwro te:
I have to extract a xml file line by line. Here is my xml
Step1: <category name= "name" guid="{guid}"st ate="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(t xtFirstMonitorP ack.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.co m>
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
3092
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 this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the current user) that tells me when the user last read items in the aggregated database.
40
61233
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
7
6441
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, received.Length); string textToAdd = Encoding.ASCII.GetString(received, 0, readBytes); myCompleteMessage = String.Concat(myCompleteMessage, textToAdd); }
3
12412
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 I call Console.Read() again?
1
2275
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
8134
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
3379
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 file aren't really useful to a user, I use the IStream::Read function together with a StreamWriter to extract single files from my compound document. When I first tested these functions everything seemed to work fine (and basically, it does),...
11
3354
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 called that line of info does not exist anymore in the file. for example i have created a short example of where i am at, but I dont know how to alter what im doing so that i just read in a single line
9
3014
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 of the above after the file is loaded into the RichTextBox, and I am trying to speed the process up by doing it in a temp file.
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.