By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,992 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,992 IT Pros & Developers. It's quick & easy.

XML - read thru the values

P: n/a
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
May 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Dan Aldean" <da*******@yahoo.com> wrote:
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
May 12 '06 #2

P: n/a


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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 12 '06 #3

P: n/a
Thanks to you all for the help.
May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.