XML and C#

Discussion in 'OT Technology' started by zanyspy_dude, May 19, 2006.

  1. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    I have a document:
    <?xml version="1.0" ?>
    <Game>
    <dot>
    <row>3</row>
    <col>3</col>
    <team>0</team>
    </dot>
    </Game>

    I want to pull out all the dots, and then grab the row, col and team. I can find the row, col, and team, but when i do:

    if (r.LocalName.Equals("col"))
    col = Convert.ToInt32(r.Value);

    i get the debugger telling me that r.value is "". r is the XMLTextReader. how do i get the 3 out? I'll copy the full code below. Thank you very much for looking :big grin:
    Code:
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                String path = openFileDialog1.FileName.ToString();
                if (path == "openFileDialog1")
                    return;
                theString = path;
                //try
                {
                    // ---- Read objects from XML-file -----
                    XmlTextReader r = new XmlTextReader(path);
                    r.WhitespaceHandling = WhitespaceHandling.None;
                    int row = -1;
                    int col = -1;
                    int team = -1;
                    r.Read();
                    if (r.NodeType != XmlNodeType.XmlDeclaration) throw new ApplicationException("No XML declaration node");
                    while (r.Read())
                    {
                        if (r.NodeType == XmlNodeType.Element)
                        {
                            if (r.LocalName.Equals("dot"))
                            {
                                Boolean done = false;
                                if (r.Read() && !done)
                                {
                                    if (r.LocalName.Equals("col"))
                                        col = Convert.ToInt32(r.Value);
                                    if (r.LocalName.Equals("row"))
                                        row = Convert.ToInt32(r.Value);
                                    if (r.LocalName.Equals("team"))
                                        team = Convert.ToInt32(r.Value);
                                    if (row != -1 && col != -1 && team != -1)
                                    {
                                        gameBoard[row, col] = team;
                                        done = true;
                                        row = -1;
                                        col = -1;
                                        team = -1;
                                    }
                                }
                            }
                        }
                    }
                }
                //catch
                {
                    MessageBox.Show("Unable to open game from file.");
                }
            }
        }
     
    Last edited: May 19, 2006
  2. hsmith

    hsmith OT Supporter

    Joined:
    Feb 24, 2002
    Messages:
    124,570
    Likes Received:
    658
    Location:
    Your mother.
    ok...?
     
  3. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    I was trying to hit shift tab and that submited before i was ready, sorry!
     
  4. hsmith

    hsmith OT Supporter

    Joined:
    Feb 24, 2002
    Messages:
    124,570
    Likes Received:
    658
    Location:
    Your mother.
    xpath is your friend

    Code:
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(txt);
                int row = int.Parse(doc.SelectSingleNode("//row").InnerText);
                int col = int.Parse(doc.SelectSingleNode("//col").InnerText);
                int team = int.Parse(doc.SelectSingleNode("//team").InnerText);
    
     
  5. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    i love you so much right now :big grin:
     

Share This Page