Xml Read Single Line at a time using C#

S

sk.rasheedfarhan

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.
 
T

Tom Porterfield

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);
}
}
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top