Re: What up with this?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Derek Harmon (loresayer_at_msn.com)
Date: 01/02/05


Date: Sat, 1 Jan 2005 19:01:04 -0500


"Anonymous" <ByteMe@discussions.microsoft.com> wrote in message news:C67757C5-973D-4821-9DD4-65F4AEC1D99C@microsoft.com...
> case XmlNodeType.Element:
> string test=SettingsReader.Value;
: :
> Why is the string test always empty?

The current node is an XML Element, and cannot have a value. There are two
good reasons for this.

1. It's consistent with the definition of an XML element in the W3C's DOM
   Specification. Even though an XmlReader isn't a DOM implementation,
   this consistency lets developers leverage their familiarity with comparable
   XML API's when working with XmlReader.

2. The real reason is because XmlReader is a streaming reader, it hasn't
   read the children of the XML element yet. SettingsReader can't know at
   this point whether the XML element contains a whole subtree of other
   elements, or one simple text child node. In order to determine this, you
   must keep reading.

See the documentation of the HasValue property to determine which node
types can actually possess a Value,

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlreaderclasshasvaluetopic.asp

What you probably mean to obtain in the string, test, is the value of the child
XML Text node of this element. The following technique shows one way
retrieve the 'text content' (in the XPath-sense) beneath an element, named
in some variable, strName (that's best fetched from the SettingsReader's
NameTable, although this example isn't that concerned with perf, e.g.,
the string concatenations instead of a formal StringBuilder.)

case XmlNodeType.Element:
    if ( SettingsReader.LocalName == strName )
    {
        strTest = "";
        flagCapture = true;
    }
    break;

case XmlNodeType.EndElement:
    if ( SettingsReader.LocalName == strName )
    {
        RememberSetting( strName, strTest);
        flagCapture = false;
    }

case XmlNodeType.Text:
    if ( flagCapture )
    {
        strTest += SettingsReader.Value;
    }

Derek Harmon



Relevant Pages

  • RE: Web Method Parameters
    ... Do you know which parameter is causing the null reference exception? ... possible that the string is NOT an XML element (this would be unusual, ... >After having used SetBodyObject to serialize a string into the body (after ... >> payload before it gets encrypted on the wire to verify this, as the SSL ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: subroutines with XML
    ... > references to Perl subroutines. ... The advice usually goes on "...use a hash instead". ... they will stringify to a unique string. ... Later, given an XML element $element, you can call the code like this: ...
    (comp.lang.perl.misc)
  • Re: old to new
    ... use a loop to compare each string to the value you're searching for. ... If you find it store the array position in a variable you can use later. ... -- Couldn't help but notice you're 'assuming' the xml element will be ...
    (microsoft.public.scripting.vbscript)