Xml Read Single Line at a time using C#

  • Thread starter Thread starter sk.rasheedfarhan
  • Start date Start date
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.
 
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);
}
}
 
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.
 
Back
Top