locked
Checking if an element has a value RRS feed

  • Question

  • Suppose I have the following xml document

    <element1>

         <element2>"value1"</element2>

         <element3>"value2"</element3>

    </element1>

    <element4>

        <element5>"value3"</element5>

    </element4.

    I am trying to use xmltextreader to parse the value1, value2, and value3.

    My code looks something like this.

    while not xml.eof

       xml.read

           If xml.IsStartElement

                if xml.value <> ""

                     dim temp1 as string = xml.localname

                     dim temp2 as string = xml.readstring

                end if

          end if

    end while

     

    as I tried to debug this xml.value is "" when xml.localname = "element2".   But I want to see that the value of element2 is "value1".  Am I using the xml.value function correctly?  Is it because a reference is being made to <element2> and that it doesn't yet see the value of  <element2>?

    Thank you.

    jche

     

    Monday, March 5, 2007 3:05 PM

Answers

  • With XmlReader an element node does not have a value, see http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader.value.aspx.

    For a short document like you have and your purpose you can easily work with XPathDocument/XPathNavigator, here is a C# sample that iterates over all elements that do not have child elements and outputs the XPath value (which is the string contents) of those elements:

    XPathDocument doc = new XPathDocument(@"XMLFile1.xml");

    XPathNavigator navigator = doc.CreateNavigator();

    XPathNodeIterator nodeIterator = navigator.Select(@"//*[not(*)]");

    while (nodeIterator.MoveNext())

    {

    Console.WriteLine("Element with Name \"{0}\" has value {1}.", nodeIterator.Current.Name, nodeIterator.Current.Value);

    }

    Output would be like

    Element with Name "element2" has value "value1".
    Element with Name "element3" has value "value2".
    Element with Name "element5" has value "value3".

     

     

    Monday, March 5, 2007 3:33 PM
  •  jche wrote:

    But I'm still curious, so is it possible to get "value1" when you are currently on <element2> or the only way to see "value1" is to use xml.read to reference to "value1"? 

    With XmlReader, as I have tried to point out, the Value property gives an empty string when the reader is positioned on an element node. As the reader works in forwards only pull parsing mode you have to read on to read out the element contents. Alternatively, if you know the elements which have text only content then you can use the ReadElementString method e.g.

      if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "element2")

      {

         Console.WriteLine(xmlReader.ReadElementString());

      }

    Monday, March 5, 2007 4:30 PM

All replies

  • With XmlReader an element node does not have a value, see http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader.value.aspx.

    For a short document like you have and your purpose you can easily work with XPathDocument/XPathNavigator, here is a C# sample that iterates over all elements that do not have child elements and outputs the XPath value (which is the string contents) of those elements:

    XPathDocument doc = new XPathDocument(@"XMLFile1.xml");

    XPathNavigator navigator = doc.CreateNavigator();

    XPathNodeIterator nodeIterator = navigator.Select(@"//*[not(*)]");

    while (nodeIterator.MoveNext())

    {

    Console.WriteLine("Element with Name \"{0}\" has value {1}.", nodeIterator.Current.Name, nodeIterator.Current.Value);

    }

    Output would be like

    Element with Name "element2" has value "value1".
    Element with Name "element3" has value "value2".
    Element with Name "element5" has value "value3".

     

     

    Monday, March 5, 2007 3:33 PM
  • Thanks alot.  I can give XPathDocument a shot.  But I'm still curious, so is it possible to get "value1" when you are currently on <element2> or the only way to see "value1" is to use xml.read to reference to "value1"? 

    Thank you for your time.

    jche

    Monday, March 5, 2007 4:07 PM
  •  jche wrote:

    But I'm still curious, so is it possible to get "value1" when you are currently on <element2> or the only way to see "value1" is to use xml.read to reference to "value1"? 

    With XmlReader, as I have tried to point out, the Value property gives an empty string when the reader is positioned on an element node. As the reader works in forwards only pull parsing mode you have to read on to read out the element contents. Alternatively, if you know the elements which have text only content then you can use the ReadElementString method e.g.

      if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "element2")

      {

         Console.WriteLine(xmlReader.ReadElementString());

      }

    Monday, March 5, 2007 4:30 PM
  • Thank you very much.  Your help is greatly appreciated.

    jche

    Monday, March 5, 2007 6:29 PM