469,962 Members | 2,341 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,962 developers. It's quick & easy.

XmlTextReader - Confusion

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!!!
Mar 29 '07 #1
2 4509
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
"JimHeavey" <Ji*******@discussions.microsoft.comwrote in message
news:27**********************************@microsof t.com...
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!!!


Mar 29 '07 #2
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.

Mar 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andy Neilson | last post: by
5 posts views Thread by Geoff Bennett | last post: by
1 post views Thread by CLL | last post: by
2 posts views Thread by Yuriy | last post: by
2 posts views Thread by XML reading with XMLTextReader | last post: by
4 posts views Thread by CodeRazor | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.