XmlTextReader - Confusion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Working with my first XML file and the first time I have used the
XmlTextReader class. Reading a book on this subject, I have constructed code
which I thought would navigate thru the file and retrieve the values for each
of the "nodes".
Running the debugger, I can see that I am navigating to each of the nodes,
but when I look at the "Value" property, it is empty. I'm not sure what I am
doing wrong.

Here is a snipit of my XML file looks like the following....

<Program>
<ProgramName>PG8NS000</ProgramName>
<Description>Update Activity on Final Approval - unable to locate specs
- see TSO90(DPG00000.OBSOLETE.APSPROG)</Description>
<Location></Location>
<Status>Inactive</Status>
</Program>
<Program>
<ProgramName>PG9SB009</ProgramName>
<Description>Remove Errors on O&B Except D0001</Description>
<Location>../Attachments/Specs/Archive/PS_PG9SB009.doc</Location>
<Status>Inactive</Status>
</Program>

Snipit of my code looks like the following....

while (rdr.Read())
{
if (rdr.NodeType == XmlNodeType.Text)
{
switch (rdr.Name)
{
case "Program":
row = _DtPrograms.NewRow();
break;
case "ProgramName":
row["ProgramName"] = rdr.Value;
break;
case "Description":
row["Description"] = rdr.Value;
break;
case "Status":
row["Status"] = rdr.Value;
break;
case "Location":
row["Location"] = rdr.Value;
break;
}
}
else if (rdr.NodeType == XmlNodeType.EndElement && rdr.Name ==
"Program")
{
_DtPrograms.Rows.Add(row);
}}


Thanks in advance for your assistance!!!
 
Each text piece is actually a node itself, so your switch case would
never work, since you are testing it for node names that belong to the
text node's parents. Put a default case in the switch clause and
you'll see what i mean.
You can change the logic to maybe remove the If clause //(if
(rdr.NodeType == XmlNodeType.Text) )
and use something like
while (rdr.Read())
{
switch (rdr.Name)
{
case "ProgramName":
row["ProgramName"] = rdr.ReadInnerXml();
break;

or other similar methods like rdr.ReadContentAsString() , whichever
suits your needs. It all depends on your schema definition.
 
XmlReader is a forward-only reader. Your xml file:

<Program>
<ProgramName>PG8NS000</ProgramName>
<Description>Update Activity on Final Approval - unable to locate specs -
see G00000.OBSOLETE.APSPROG)</Description>
<Location>New York</Location>
<Status>Inactive</Status>
</Program>

can be read using the following code (assuming that it is saved as
test.xml):

StreamReader streamReader = new StreamReader("c:\\test.xml");
string xmlData = streamReader.ReadToEnd();
streamReader.Close();

XmlTextReader xmlReader = new XmlTextReader(new StringReader(xmlData));
xmlReader.WhitespaceHandling = WhitespaceHandling.None;
xmlReader.MoveToContent();
xmlReader.Read();

string programName = xmlReader.ReadElementString();
string desc = xmlReader.ReadElementString();
string location_ServiceCallPhoned = xmlReader.ReadElementString();
string status = xmlReader.ReadElementString();

xmlReader.Close();

Regards,
tankc
 
Back
Top