views:

14

answers:

1

I current have the following XML fragment that I'm parsing to read its values and display them on a web page:

<root>
  <PostcardTitle>The Needles</PostcardTitle>
  <PostcardDescr>Granite Dells, Prescott</PostcardDescr>
  <PostcardImage>
    <img alt="GraniteDells" src="GraniteDells.jpg" />
  </PostcardImage>
  <PostcardDate />
  <PostcardCredit>Photo Courtesy of Joe Schmoe</PostcardCredit>
</root>

I'm doing this through the following code (reading fragment from the database):

Dim ContentDoc As XDocument
Dim DocPoints As IEnumerable(Of XElement)

ContentDoc = XDocument.Parse(ContentData.Item(0).content)
DocPoints = ContentDoc.<root>.Elements()

   For Each Item As XElement In DocPoints

   Dim TempLabel As New Label
   TempLabel.Text = Item.Name.LocalName & ": " & Item.Value

   Dim TempLiteral As New Literal
   TempLiteral.Text = "<br/><br/>"

   pnlEditItems.Controls.Add(TempLabel)
   pnlEditItems.Controls.Add(TempLiteral)

Next

When the script runs, all nodes are successfully parsed and displayed, including the empty PostcardDate node, except for the PostcardImage node. When debugging, evaluating Item.Value returns an empty string. However, evaluating Item.ToString() returns the PostcardItem tags and the value within it.

Is there something that needs to be done to have the XElement object read multi-line values, or am I missing something else?

+2  A: 

You're missing what XElement.Value is documented to return:

A String that contains all of the text content of this element. If there are multiple text nodes, they will be concatenated.

Your PostcardImage document doesn't have any text content (other than whitespace). You should work out which bits of it you want (e.g. the alt attribute of the img tag) and use that. Alternatively, if you just want to embed the XML verbatim, you may want to just call Nodes() on the element... I don't think there's any equivalent of the "InnerXml" property of the DOM.

Jon Skeet
I completely forgot that it would be reading the image tag as a node. Grabbing the first node of the element gave me the complete image tag text. As always Jon, you're awesome! Thanks!!!
Dillie-O