Skip to content

Error while reading XML using XMLTextReader

Prasad Kulkarni

Asked Jun 25, 2009, 9:12 PM EDT2 answers
Share
LinkedInX
hi all
        i am reading XML using XMLTextReader but can't get whole line

my XML is as follows
[CODE]This is test XML yes [/CODE]
when i read
XmlReaderObject.Value then i get only [B]This is text[/B]
but i want  whole line as
[B]This is test XML yes [/B]
how can i code it ???
any hint ??
thankx in advance
koolprasad2003:)

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Niradhip Chakraborty
    +1
    It is because after "This is test" you have written XML and it will consider
    as a different element and it is not giving you the text inside you will get the text inside in next iteration. So you can hold the previous value in string and concatinate with the new value to get the entire string.
    Try the following method
    Check my xml file:


     [B]This is test XML yes [/B]

    while (reader.Read())
    {
       if (reader.NodeType == XmlNodeType.Element)
         {
            strNodeName = reader.Name;
         }
       if (reader.NodeType == XmlNodeType.Text)
       {
           if (strNodeName == "MayPara")
            {
                strvalue = reader.Value.ToString();
            }
            if(strNodeName == "b")
            {
               if((strNodeName !="")
               {
                  strNodeName = strNodeName + reader.Value.ToString();
               }
            }
       }

    }
    +1
  • Niradhip Chakraborty
    +1
    Or if you want to stick with your code and dont want to concatenate then
    instead of use >
    Your xml code: This is test XML yes
    Revised xml code: This is test <b>XML</b&gt yes
    +1