XML - read thru the values

D

Dan Aldean

Hi,

I create this XML document:
<?xml version="1.0"?>
<Bookstore>
<Book Genre="Novel" Style="hardcover">
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<Title>The Handmaid's Tale</Title>
<Price>$19.25</Price>
</Book>
<Book Genre="Novel" Style="hardcover">
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<Title>The Poisonwood Bible</Title>
<Price>$11.99</Price>
</Book>
</Bookstore>


Then I try to read and get the values from it using this code:

XmlTextReader xmlTextReader = new XmlTextReader("Sample.xml");

while (xmlTextReader.Read())

{

if (xmlTextReader.NodeType == XmlNodeType.Element)

lstBox.Items.Add(xmlTextReader.Value);

}

It only returns empty values. I would have expected for example:

first-name element to be Margaret



Thanks
 
B

Barry Kelly

Dan Aldean said:
if (xmlTextReader.NodeType == XmlNodeType.Element)

lstBox.Items.Add(xmlTextReader.Value);

}

It only returns empty values. I would have expected for example:

first-name element to be Margaret

Have you tried reading the documentation for XmlTextReader.Value? I'll
quote it:

---8<---
The value returned depends on the NodeType of the node. The following
table lists node types that have a value to return. All other node types
return String.Empty.

Node Type
Value

Attribute
The value of the attribute.

CDATA
The content of the CDATA section.

Comment
The content of the comment.

DocumentType
The internal subset.

ProcessingInstruction
The entire content, excluding the target.

SignificantWhitespace
The white space within an xml:space= 'preserve' scope.

Text
The content of the text node.

Whitespace
The white space between markup.

XmlDeclaration
The content of the declaration.

--->8---

That seems pretty clear to me. You should be looking for a Text node as
a child of the Element node.

-- Barry
 
M

Martin Honnen

Dan Aldean wrote:

<Bookstore>
<Book Genre="Novel" Style="hardcover">
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<Title>The Handmaid's Tale</Title>
<Price>$19.25</Price>
</Book>
<Book Genre="Novel" Style="hardcover">
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<Title>The Poisonwood Bible</Title>
<Price>$11.99</Price>
</Book>
</Bookstore>


Then I try to read and get the values from it using this code:

XmlTextReader xmlTextReader = new XmlTextReader("Sample.xml");

while (xmlTextReader.Read())

{

if (xmlTextReader.NodeType == XmlNodeType.Element)

lstBox.Items.Add(xmlTextReader.Value);

}

It only returns empty values. I would have expected for example:

first-name element to be Margaret

element nodes have no value respectively an empty value. You can check
the node's name and then call ReadString to read the string contents if
you know the element has simple content e.g.

XmlTextReader xmlReader = new XmlTextReader(@"file.xml");
while (xmlReader.Read()) {
switch (xmlReader.NodeType) {
case XmlNodeType.Element:
switch (xmlReader.Name) {
case "first-name":
goto case "last-name";
case "Title":
goto case "last-name";
case "Price":
goto case "last-name";
case "last-name":
Console.WriteLine("{0}: {1}", xmlReader.Name,
xmlReader.ReadString());
break;
}
break;
}
}

will yield

first-name: Margaret
last-name: Atwood
Title: The Handmaid's Tale
Price: $19.25
first-name: Barbara
last-name: Kingsolver
Title: The Poisonwood Bible
Price: $11.99
 

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

Similar Threads


Top