Reading from an XML file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading from an XML file

[b][red]This message was edited by shinsnake at 2006-8-31 19:53:19[/red][/b][hr]
I'm sure this is pretty simple, but I've got this XML file:

http://xoap.weather.com/search/search?where=Columbus, OH

[code]<?xml version="1.0" encoding="ISO-8859-1" ?>
-

Columbus, OH
Columbus/Bolton Field, OH
Columbus/OSU Arpt, OH
Columbus/Rickenbacker ANGB, OH
[/code]

Now, in my C# ASP.NET code behind file, I use the following code:

[code]XmlTextReader xtrLocCode = new XmlTextReader("http://xoap.weather.com/search/search?where=" + txtCity.Text.ToString() + ",%20" +
ddlState.Text.ToString());
xtrLocCode.WhitespaceHandling = WhitespaceHandling.None;
StringBuilder strText = new StringBuilder();

while (xtrLocCode.Read())
{
strText.Append(xtrLocCode.GetAttribute("id") + "
");
}[/code]

Sometimes, you get only one location id and other times you get multiple. How do I determine if I have multiple loc id's? What I plan to do is if I have multiple create a dropdownlist with each id location's text and have the user select one.

Comments

  • infidelinfidel Posts: 2,900Member
    : [code]XmlTextReader xtrLocCode = new XmlTextReader("http://xoap.weather.com/search/search?where=" + txtCity.Text.ToString() + ",%20" +
    : ddlState.Text.ToString());
    : xtrLocCode.WhitespaceHandling = WhitespaceHandling.None;
    : StringBuilder strText = new StringBuilder();
    :
    : while (xtrLocCode.Read())
    : {
    : strText.Append(xtrLocCode.GetAttribute("id") + "
    ");
    : }[/code]
    :
    : Sometimes, you get only one location id and other times you get multiple. How do I determine if I have multiple loc id's? What I plan to do is if I have multiple create a dropdownlist with each id location's text and have the user select one.

    Is that really how the XmlTextReader.Read method is supposed to be used? Looks suspicious to me.

    What you should do is load the XML into a DOM object and use an XPath to select all of the "loc" elements:

    [code]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim _url As String = "http://xoap.weather.com/search/search?where=Phoenix, AZ"
    Dim _doc As New Xml.XmlDocument()
    Dim _locs As Xml.XmlNodeList
    Dim _loc As Xml.XmlElement

    _doc.Load(_url)
    _locs = _doc.SelectNodes("/search/loc")
    For Each _loc In _locs
    Me.ComboBox1.Items.Add(_loc.InnerText)
    Next

    End Sub
    [/code]

    Ok, I went and looked up the XmlTextReader and for your application I don't think it's appropriate. The main problem is that it returns each "node", one at a time, from the XML just like a SAX parser does (or at least very similar). The difficult part is that a string of text in XML is also a node, so when XmlTextReader.Read() leaves you on a text node, you don't have any easy way of telling that the text node's parent is or is not a "loc" element.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • shinsnakeshinsnake Posts: 93Member
    : Is that really how the XmlTextReader.Read method is supposed to be used? Looks suspicious to me.
    :
    : What you should do is load the XML into a DOM object and use an XPath to select all of the "loc" elements:
    :
    : [code]
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    :
    : Dim _url As String = "http://xoap.weather.com/search/search?where=Phoenix, AZ"
    : Dim _doc As New Xml.XmlDocument()
    : Dim _locs As Xml.XmlNodeList
    : Dim _loc As Xml.XmlElement
    :
    : _doc.Load(_url)
    : _locs = _doc.SelectNodes("/search/loc")
    : For Each _loc In _locs
    : Me.ComboBox1.Items.Add(_loc.InnerText)
    : Next
    :
    : End Sub
    : [/code]
    :
    : Ok, I went and looked up the XmlTextReader and for your application I don't think it's appropriate. The main problem is that it returns each "node", one at a time, from the XML just like a SAX parser does (or at least very similar). The difficult part is that a string of text in XML is also a node, so when XmlTextReader.Read() leaves you on a text node, you don't have any easy way of telling that the text node's parent is or is not a "loc" element.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

    Yeah, I've since given up on XMLTextReader and I'm trying the method you suggested. The XMLTextReader worked fine for one location id, but once I got past one, I started putting a ~ in between each read and then used a strText.Remove command to remove all but the first code. It worked, but obviously, not a good way to do it.

    I'll try your code and see what I can monkey around with to work. Thanks!
  • shinsnakeshinsnake Posts: 93Member
    Okay, this is what I came up with:

    [code]string strLocCode = "";
    StringBuilder strText = null;

    string url = "http://xoap.weather.com/search/search?where=" + txtCity.Text.ToString() + ",%20" + ddlState.Text.ToString();
    XmlDocument xdocLocCode = new XmlDocument();
    XmlNodeList xnlistLocCode;

    xdocLocCode.Load(url);
    xnlistLocCode = xdocLocCode.SelectNodes("/search/loc");

    foreach (XmlElement xelLocCode in xnlistLocCode)
    {
    if (xnlistLocCode.Count == 1)
    {
    txtLocCode.Text = xelLocCode.GetAttribut("id");
    strLocCode = txtLocCode.Text;
    }
    else
    {

    }
    }[/code]

    This is what I went with. I needed to get the Location id, the inner text isn't that important to me because to do my second XML call, I need that 8 digit code. Thanks for all of your help.
Sign In or Register to comment.