Groups | Blog | Home
all groups > dotnet xml > november 2005 >

dotnet xml : Can't aquire element's value if it hosts sub-elements


DesperateDan
11/30/2005 4:37:52 AM
As the title say, I can't aquire element's value if it hosts
sub-elements.

Let me explain - My XML looks like this:-
<a>
<infill>xxxxxx</infill>
<SourceEndPoint>testdata
<EndPoint>
<AliasRef ref="lAlias120001">
<Qualifier><![CDATA[OrganizationName]]></Qualifier>
<Value><![CDATA[Home Organization]]></Value>
</AliasRef>
<OrgName ref="lOrg110001"><![CDATA[Home
Organization]]></OrgName>
</EndPoint>
</SourceEndPoint>
</a>
When "pointing" to element <SourceEndPoint> via MoveToNext method the
Value property is set to:
"OrganizationNameHome OrganizationHome Organization"
Which is correct - A concatenation of child node values.

I want the data associated with <SourceEndPoint> element (i.e.
"testdata")

Can someone help? (Bear in mind that moving to parent then means your
first child is <infill> element)

Dan
Truong Hong Thi
11/30/2005 8:45:36 PM
[quoted text, click to view]
I suggest you try the following:
When "pointing" to element <SourceEndPoint>, you call Read() again to
move to the first child text element, then get its value.

Thi
Peter Flynn
11/30/2005 11:36:42 PM
[quoted text, click to view]

This is usually A Very Bad Idea in non-text documents. It's called Mixed
Content, and it means the content of the SourceEndPoint element is made
up of both text (testdata) *and* other elements (EndPoint). This is the
normal usage in text documents (eg HTML paragraphs, where you can mix
text and subelements) but it's almost always a poor design decision in
"data" documents. It would be easier to say

<SourceEndPoint>
<Data>testdata</Data>
<EndPoint>
...

If you absolutely can't change the markup (eg it's been sicced on you
by someone who doesn't understand XML) then the text content can be
accessed as the first text-node in the SourceEndPoint element. In XSLT
this would be the XPath expression SourceEndPoint/text()[1] (and bear
in mind that the returned value will include the newline after the text
*and* the following spaces up to the start of <EndPoint>: this is one
of the reasons why allowing unmarked data to float around the place not
properly identified is not good design).

///Peter
--
XML FAQ: http://xml.silmaril.ie/
AddThis Social Bookmark Button