WPF - Finding XML Document for Values ​​using XMLTextReader

Open up another WPF question, well I guess it's just generic .NET. I have an XML document obtained from a url.

I want to get multiple values ​​from a document (weather data, location, some other strings).

When I use XmlTextReader, I can call my method to pull the values. The first time I pass the xml node search method and get the value (XMLTextReader object) I get the data I want back, but then the XMLTextReader is dead. Not sure why this is happening. So I need to do this UGLY code below in FindTags method .... I just want to continue passing xtr (XMLTextreader) back to find method. Is this the character of the reader? I don't want to hit the url every time ... seems wrong too.

Help .. it just doesn't feel right.

Thank.

        GetWeatherFeed("97229", "//weather//loc//dnam", "//weather//cc//tmp", "/weather/cc/icon");

      

Get WeatherFeed method (cut off)

        System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(Url that retuns xm);
        System.Collections.Hashtable ht = new System.Collections.Hashtable();

        ht = FindTagsUsingXPthNaviatorAndXPathDocumentNew(xtr, location, temperature, iconid);
        lblLocation.Content = ht["Location"].ToString();
        lblWeatherCondition.Content = ht["Weather"].ToString();


public System.Collections.Hashtable FindTagsUsingXPthNaviatorAndXPathDocumentNew(System.Xml.XmlTextReader xtr, string nodeToLocate1, string nodeToLocate2, string nodeToLocate3)
{
    System.Xml.XPath.XPathDocument xpDoc = new System.Xml.XPath.XPathDocument(xtr);
    System.Xml.XPath.XPathNavigator xpNav = xpDoc.CreateNavigator();
    System.Xml.XPath.XPathExpression xpExpression = xpNav.Compile(nodeToLocate1);

    System.Xml.XPath.XPathNodeIterator xpIter = xpNav.Select(xpExpression);
    System.Collections.Hashtable ht = new System.Collections.Hashtable();

    while (xpIter.MoveNext())
    {
        ht.Add("Location", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate2);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Weather", xpIter.Current.Value);
    }

    xpExpression = xpNav.Compile(nodeToLocate3);

    xpIter = xpNav.Select(xpExpression);
    while (xpIter.MoveNext())
    {
        ht.Add("Icon", xpIter.Current.Value);
    }

    return ht;
}

      

0


source to share


3 answers


The XmlTextReader cannot be reset to the beginning. Load the content first and then use multiple XmlTextReaders (if you need).



If the document being loaded is small, I would just use XmlDocument (or XDocument if you are using .NET 3.5)

0


source


Here's what I did ... great answer.



            System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(my xml url);
            System.Xml.XPath.XPathDocument xdoc = new System.Xml.XPath.XPathDocument(xtr);

            lblLocation.Content = getXmlNodeValue(xdoc, location);
            lblWeatherCondition.Content = getXmlNodeValue(xdoc, temperature);

      

+1


source


Doesn't XMLTextReader read SAX? Don't you need to rewind the stream to read the file again?

0


source







All Articles