What Xquery to write for xml in C#?

Last post 05-09-2008 9:52 AM by Bonekrusher. 1 replies.

Sort Posts:

  • What Xquery to write for xml in C#?

    05-09-2008, 9:21 AM

    i have an xml file of calendar. Below is the details of it.. 

    <calender>
      <appointment>
        <date>Friday, May 09, 2008</date>
        <time>12:58:30 PM</time>
        <title>Conference Call</title>
        <description>asccfasfas</description>
      </appointment>
      <appointment>
        <date>Wednesday, May 21, 2008</date>
        <time>6:46:04 PM</time>
        <title>Conference Call</title>
        <description>asdadadadasdasdas</description>
      </appointment>
      <appointment>
        <date>Wednesday, May 21, 2008</date>
        <time>10:46:12 PM</time>
        <title>Dinner</title>
        <description>asdasdadada</description>
      </appointment>
    </calender>
    Now i want to select appointments according to Date. Now i have an Xquery which selects only one appointment for that date. say for 21st date it displays the first appointment stores in the textbox nd displays in the dataGridView. i will post the code below....... 
    MessageBox.Show(date);
    			textBox1.Text=date;
    
                string mainPath = Path.Combine(instance.MainPath, instance.User);
    
               string FILE_NAME = Path.Combine(mainPath, "calender.xml");
                // check if file exists
    
                 string[] filenames1 = Directory.GetFiles(mainPath);
                    bool flagg1 = false;
                    foreach (string nm in filenames1)
                        if (nm.Contains("calender.xml"))
                            flagg1 = true;
                    if (flagg1 == true)  //outbox.xml exists
                    {
                        XPathDocument doc;
                        XPathNavigator nav;
                        XPathExpression expr;
                        XPathNodeIterator iterator;
                        doc = new XPathDocument(FILE_NAME);
                        nav = doc.CreateNavigator();
                        string str = textBox1.Text;
                        if (str == "") return;
                        string oldTitle = "";
                        oldTitle = str;
                        expr = nav.Compile("/calender/appointment[date='" + str + "']");
                        iterator = nav.Select(expr);
    
                        if (iterator.MoveNext())
                        {
    
                            XPathNavigator nav2 = iterator.Current.Clone();
    
    
    
                            nav2.MoveToFirstChild();
                            textBox2.Text = nav2.Value;
                            nav2.MoveToNext();
                            textBox3.Text = nav2.Value;
                            nav2.MoveToNext();
                            textBox4.Text = nav2.Value;
                            nav2.MoveToNext();
                            richTextBox1.Text = nav2.Value;
                        }
    
    
                        string value1 = this.richTextBox1.Text;
                        instance.Des = richTextBox1.Text;
    
    
                        description des = new description();
                                           
                        MainForm mf = (MainForm)this.FindForm();
                        mf.splitContainer2.Panel2.Controls.Add(des);
                        des.Dock = DockStyle.Fill;
                        des.BringToFront();
    
                        ///
                        XmlDocument docu = new XmlDocument();
    
                        string filename = Path.Combine(mainPath ,"datagrid.xml");
                        docu.Load(filename);
    
    
                        DataSet dsAuthors = new DataSet("datagrid");
    
                        //docu.Load(filename);
                        XmlNode node = docu.DocumentElement;
    
                        XmlNode Appoint = docu.DocumentElement;
                        XmlElement Appointment = docu.CreateElement("appointment");
    
                        XmlElement Date = docu.CreateElement("date");
                        Date.InnerText = textBox2.Text;
                        XmlElement Time = docu.CreateElement("time");
                        Time.InnerText = textBox3.Text;
                        XmlElement Title = docu.CreateElement("title");
                        Title.InnerText = textBox4.Text;
                        node.AppendChild(Date);
                        node.AppendChild(Time);
                        node.AppendChild(Title);
                        docu.Save(filename);
    
    
    
    
    
    
                        XPathDocument docm = new XPathDocument(filename);
                        XPathNavigator navigator = docm.CreateNavigator();
                        XPathNodeIterator node1 = navigator.Select("/datagrid/date");
                        XPathNodeIterator node2 = navigator.Select("/datagrid/time");
                        XPathNodeIterator node3 = navigator.Select("/datagrid/title");
    
    
                        //
                        // TODO: Add constructor code after the InitializeComponent() call.
                        //
    
                        string s, p, q;
    
                        while (node1.MoveNext() & node2.MoveNext() & node3.MoveNext())
                        {
    
                            DataGridViewRowCollection rw = dataGridView1.Rows;
    
                            s = node1.Current.InnerXml;
                            p = node2.Current.InnerXml;
                            q = node3.Current.InnerXml;
                            //navigator.MoveToRoot();        
                            rw.Add(s, p, q);
    
                        }
    
    
    
                        //clear xml file
                        node.RemoveAll();
                        docu.Save(filename);
    
                        //dataGridView1;

     How do i select both the appoinments of the same date?

    waitn for an early reply in c#.Net nd thanks in advance 

  • Re: What Xquery to write for xml in C#?

    05-09-2008, 9:52 AM
    Answer

     Something like this:

     

      int i = 0;
         XPathDocument xpathDoc;
         XPathNavigator xmlNav;
        XPathNodeIterator xmlNI;
         try {
             xpathDoc = new XPathDocument("c:/temp/mail.xml");
            xmlNav = xpathDoc.CreateNavigator();
             xmlNI = xmlNav.Select("/calender/appointment/date[contains(., 'May 21, 2008')]");
             while ((xmlNI.MoveNext())) {
                 Interaction.MsgBox(xmlNI.Current.Value);
                 i = i + 1;
            }
             this.TextBox1.Text = "total hits: " + i.ToString;
         }
         catch (Exception ex) {
             Interaction.MsgBox(Err.Description);
        }
      
    If I was helpful, please mark "answered" so I can get credit. Thanks!
Page 1 of 1 (2 items)