XML to LINQ start the search from root

Last post 11-03-2009 2:14 PM by atconway. 1 replies.

Sort Posts:

  • XML to LINQ start the search from root

    10-29-2009, 3:53 PM
    • Member
      point Member
    • stopete
    • Member since 05-23-2007, 6:18 PM
    • Posts 13

    So far I don’t got problems searching elements for example , the next block of code will load the xml file and then will search descendants (“recordinfomation”) which is a parent element in the XML file and returns child elements  (“title”, “aut_FirstName, “aut_LastName, and “xmllink”) . My question is I would like to start my search at the root of the xml file and search all parent elements and not a specific one and return what I am returning in the following code.  In another words search the whole document for specific child elements.

    Dim XDoc As XElement = XElement.Load(MapPath("App_Data\Bookstore.xml"))

    Dim elements = From row In XDoc.Descendants("recordinformtion") _

                           Where Regex.IsMatch(row.Element("title").Value, strfirstterm, RegexOptions.IgnoreCase) And Regex.IsMatch(row.Element("title").Value, strsecondterm, RegexOptions.IgnoreCase) And Regex.IsMatch(row.Element("title").Value, strthirdterm, RegexOptions.IgnoreCase) _

                           Select Name = row.Element("auth_FirstName").Value, last = row.Element("aut_LastName").Value, _

                   Capital = row.Element("title").Value, strURL = row.Attribute("xmllink").Value

     

    Here is the whole code:

       Dim XDoc As XElement = XElement.Load(MapPath("App_Data\Bookstore.xml"))

             Dim strmessage As String

            Dim strfirstterm, strsecondterm, strthirdterm As String

             Dim lbl As New Label

             strfirstterm = TextBox1.Text

            strsecondterm = TextBox2.Text

            strthirdterm = TextBox3.Text

     

            Dim elements = From row In XDoc.Descendants("recordinformtion") _

                           Where Regex.IsMatch(row.Element("title").Value, strfirstterm, RegexOptions.IgnoreCase) And Regex.IsMatch(row.Element("title").Value, strsecondterm, RegexOptions.IgnoreCase) And Regex.IsMatch(row.Element("title").Value, strthirdterm, RegexOptions.IgnoreCase) _

                           Select Name = row.Element("auth_FirstName").Value, last = row.Element("aut_LastName").Value, _

                   Capital = row.Element("title").Value, strURL = row.Attribute("xmllink").Value

     

            Dim v = elements.ToList()

             strmessage = "Your search has returned no results. You might want to check spelling."

             If v.Count <= 0 Then

                 lbl.Text = "<script language='javascript'>" & Environment.NewLine & _

                        "window.alert('" + strmessage + "') </script>"

                Page.Controls.Add(lbl)

             End If

             'The datasource for the ListView1 is the strSudents

            ListView1.DataSource = elements

             'binding for the listView1 to our dataSource

            ListView1.DataBind()

  • Re: XML to LINQ start the search from root

    11-03-2009, 2:14 PM
    Answer
    • Contributor
      5,886 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,229

    I think I might be able to figure this out and post a solution, but rather than go back and forth a few times, I would reccomend re-posting this in the XML forum.

    I am newer to using Linq to XML but there are some awsome users and moderators in the ASP.NET XML forum that could probably answer this question quickly for you.

    Here is the XML forum:

    http://forums.asp.net/43.aspx

     

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

Page 1 of 1 (2 items)