Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Seraching in XML by LINQ ?

Seraching in XML by LINQ ?

Scheduled Pinned Locked Moved LINQ
csharpphpdatabaselinqcom
9 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hi there. I have a XML file like this :

    <NODE15>
    <BOOKMARK16 NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
    <BOOKMARK18 NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
    <BOOKMARK19 NAME="Mozilla Support" />
    <BOOKMARK20 HREF="http://www.download.com/" />
    <BOOKMARK21 HREF="http://firefoxplanet.ir/" />
    <BOOKMARK22 HREF="http://developer.mozilla.org/en/docs/Extensions" />
    <BOOKMARK23 HREF="http://forums.mozillazine.org/viewforum.php?f19" />
    <BOOKMARK24 HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
    </NODE15>

    I want to search in it by LINQ. I used this code

    X_Element = XElement.Load(FileName);
    var searchNodes = from nodes in X_Element.Descendants()
    where Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase)
    select nodes;

    But there is no data in searchNodes What's wrong with it ? Thanks in advance

    H 1 Reply Last reply
    0
    • M Mohammad Dayyan

      Hi there. I have a XML file like this :

      <NODE15>
      <BOOKMARK16 NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
      <BOOKMARK18 NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
      <BOOKMARK19 NAME="Mozilla Support" />
      <BOOKMARK20 HREF="http://www.download.com/" />
      <BOOKMARK21 HREF="http://firefoxplanet.ir/" />
      <BOOKMARK22 HREF="http://developer.mozilla.org/en/docs/Extensions" />
      <BOOKMARK23 HREF="http://forums.mozillazine.org/viewforum.php?f19" />
      <BOOKMARK24 HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
      </NODE15>

      I want to search in it by LINQ. I used this code

      X_Element = XElement.Load(FileName);
      var searchNodes = from nodes in X_Element.Descendants()
      where Regex.IsMatch(nodes.Attribute("HREF").Value, "Mozilla", RegexOptions.IgnoreCase)
      select nodes;

      But there is no data in searchNodes What's wrong with it ? Thanks in advance

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      I assume the code fragment was incorrectly copied: 1) X_Element should be "XElement X_Element" 2) you assume the attribute exists, which in the first bookmark results in a null reference - so I don't know how you got any results without an error The code below works:

             var X\_Element = XElement.Load("XMLfile1.xml"); 
              
              var searchNodes = from nodes in X\_Element.Descendants()
                                where nodes.Attribute("HREF") != null
                                && Regex.IsMatch(nodes.Attribute("HREF").Value, 
                                                  "Mozilla", RegexOptions.IgnoreCase) 
                                select nodes;
              Console.WriteLine("There are {0} nodes", searchNodes.Count());
              Console.ReadKey();
      

      I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:

      <NODE>
      <BOOKMARK NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
      <BOOKMARK NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
      <BOOKMARK NAME="Mozilla Support" />
      <BOOKMARK HREF="http://www.download.com/" />
      <BOOKMARK HREF="http://firefoxplanet.ir/" />
      <BOOKMARK HREF="http://developer.mozilla.org/en/docs/Extensions" />
      <BOOKMARK HREF="http://forums.mozillazine.org/viewforum.php?f19" />
      <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
      </NODE>

      'Howard

      M 1 Reply Last reply
      0
      • H Howard Richards

        I assume the code fragment was incorrectly copied: 1) X_Element should be "XElement X_Element" 2) you assume the attribute exists, which in the first bookmark results in a null reference - so I don't know how you got any results without an error The code below works:

               var X\_Element = XElement.Load("XMLfile1.xml"); 
                
                var searchNodes = from nodes in X\_Element.Descendants()
                                  where nodes.Attribute("HREF") != null
                                  && Regex.IsMatch(nodes.Attribute("HREF").Value, 
                                                    "Mozilla", RegexOptions.IgnoreCase) 
                                  select nodes;
                Console.WriteLine("There are {0} nodes", searchNodes.Count());
                Console.ReadKey();
        

        I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:

        <NODE>
        <BOOKMARK NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
        <BOOKMARK NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
        <BOOKMARK NAME="Mozilla Support" />
        <BOOKMARK HREF="http://www.download.com/" />
        <BOOKMARK HREF="http://firefoxplanet.ir/" />
        <BOOKMARK HREF="http://developer.mozilla.org/en/docs/Extensions" />
        <BOOKMARK HREF="http://forums.mozillazine.org/viewforum.php?f19" />
        <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
        </NODE>

        'Howard

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Thank you very much Howard. Is this a good way for searching in big XML file ?

        Howard Richards wrote:

        I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:

        But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute):confused:

        H 1 Reply Last reply
        0
        • M Mohammad Dayyan

          Thank you very much Howard. Is this a good way for searching in big XML file ?

          Howard Richards wrote:

          I'd also suggest if you are in control of the XML format that you need drop the naming of each element with a different name - this would be better:

          But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute):confused:

          H Offline
          H Offline
          Howard Richards
          wrote on last edited by
          #4

          Mohammad Dayyan wrote:

          But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute)

          Lets say the XML is

          <NODE>
          <BOOKMARK ID="1" NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
          <BOOKMARK ID="2" NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
          <BOOKMARK ID="3" NAME="Mozilla Support" />
          <BOOKMARK ID="4" HREF="http://www.download.com/" />
          <BOOKMARK ID="5" HREF="http://firefoxplanet.ir/" />
          <BOOKMARK ID="6" HREF="http://developer.mozilla.org/en/docs/Extensions" />

          <BOOKMARK ID="7" HREF="http://forums.mozillazine.org/viewforum.php?f19" /> <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
          </NODE>

          If you wanted to pick a specific node the easiest way is to specify ID=3 in the where clause. However, if you don't want the ID and just want the third element in the sequence:

          var X_Element = XElement.Load("XMLFile1.xml");

                  // select only bookmark descendants
                  var items = from bookmark in X\_Element.Descendants("BOOKMARK") 
                              select bookmark;
                  
                  // get third item by skipping first two
                  var third = items.Skip(2).FirstOrDefault();
          
                  if (third != null)
                  {
                      Console.WriteLine("Third item is {0}", third.Attribute("NAME").Value);
                  }
                  Console.ReadKey();
          

          This will throw an exception if there are less than three elements in then sequence so you should check items.Count() before trying to get the third.

          Mohammad Dayyan wrote:

          Is this a good way for searching in big XML file ?

          Define big! LINQ to XML using .Load will load the whole document into memory. For small applications this is usually okay. However there is a way of streaming very large XML documents in LINQ. Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML - he talks about streaming in the second half but you should find the whole thing really useful (pick the fourth video, LINQ to XML).

          'Howard

          M 1 Reply Last reply
          0
          • H Howard Richards

            Mohammad Dayyan wrote:

            But if I want to select a specific node ( for example third node) how I can select it ? (if I want to don't use attribute)

            Lets say the XML is

            <NODE>
            <BOOKMARK ID="1" NAME="Mozilla - Home" HREF="http://en-us.www.mozilla.com/en-US/" />
            <BOOKMARK ID="2" NAME="mozillaZine Forums" HREF="http://forums.mozillazine.org/index.php" />
            <BOOKMARK ID="3" NAME="Mozilla Support" />
            <BOOKMARK ID="4" HREF="http://www.download.com/" />
            <BOOKMARK ID="5" HREF="http://firefoxplanet.ir/" />
            <BOOKMARK ID="6" HREF="http://developer.mozilla.org/en/docs/Extensions" />

            <BOOKMARK ID="7" HREF="http://forums.mozillazine.org/viewforum.php?f19" /> <BOOKMARK HREF="https://addons.mozilla.org/en-US/firefox/addon/5229" />
            </NODE>

            If you wanted to pick a specific node the easiest way is to specify ID=3 in the where clause. However, if you don't want the ID and just want the third element in the sequence:

            var X_Element = XElement.Load("XMLFile1.xml");

                    // select only bookmark descendants
                    var items = from bookmark in X\_Element.Descendants("BOOKMARK") 
                                select bookmark;
                    
                    // get third item by skipping first two
                    var third = items.Skip(2).FirstOrDefault();
            
                    if (third != null)
                    {
                        Console.WriteLine("Third item is {0}", third.Attribute("NAME").Value);
                    }
                    Console.ReadKey();
            

            This will throw an exception if there are less than three elements in then sequence so you should check items.Count() before trying to get the third.

            Mohammad Dayyan wrote:

            Is this a good way for searching in big XML file ?

            Define big! LINQ to XML using .Load will load the whole document into memory. For small applications this is usually okay. However there is a way of streaming very large XML documents in LINQ. Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML - he talks about streaming in the second half but you should find the whole thing really useful (pick the fourth video, LINQ to XML).

            'Howard

            M Offline
            M Offline
            Mohammad Dayyan
            wrote on last edited by
            #5

            Howard Richards wrote:

            Lets say the XML is

            Oh , Thank you. Actually I didn't know that. :-O

            Howard Richards wrote:

            Define big!

            For example I have a XML file that there are about 1000 nodes (like above XML) in it.

            Howard Richards wrote:

            Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML

            Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)

            H 2 Replies Last reply
            0
            • M Mohammad Dayyan

              Howard Richards wrote:

              Lets say the XML is

              Oh , Thank you. Actually I didn't know that. :-O

              Howard Richards wrote:

              Define big!

              For example I have a XML file that there are about 1000 nodes (like above XML) in it.

              Howard Richards wrote:

              Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML

              Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)

              H Offline
              H Offline
              Howard Richards
              wrote on last edited by
              #6

              I personally would not class a 1000 nodes as big - almost all current PCs able to run .NET would be able to load a file that size in memory with ease. Mike Taulty's example was a 100MB XML file which could be several million lines of XML - something like that would be too large to load into memory in one go, so streaming is the only way to process it.

              'Howard

              M 1 Reply Last reply
              0
              • H Howard Richards

                I personally would not class a 1000 nodes as big - almost all current PCs able to run .NET would be able to load a file that size in memory with ease. Mike Taulty's example was a 100MB XML file which could be several million lines of XML - something like that would be too large to load into memory in one go, so streaming is the only way to process it.

                'Howard

                M Offline
                M Offline
                Mohammad Dayyan
                wrote on last edited by
                #7

                Wow. it's not big, it's huge. Thank you

                1 Reply Last reply
                0
                • M Mohammad Dayyan

                  Howard Richards wrote:

                  Lets say the XML is

                  Oh , Thank you. Actually I didn't know that. :-O

                  Howard Richards wrote:

                  Define big!

                  For example I have a XML file that there are about 1000 nodes (like above XML) in it.

                  Howard Richards wrote:

                  Watch this presentation[^] by Mike Taulty for a really nice talk on LINQ to XML

                  Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)

                  H Offline
                  H Offline
                  Howard Richards
                  wrote on last edited by
                  #8

                  Mohammad Dayyan wrote:

                  Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)

                  Ah - I could not see a way to download that video either. Try these smaller ones - I think they can [^] - these you can download and watch offline.

                  'Howard

                  M 1 Reply Last reply
                  0
                  • H Howard Richards

                    Mohammad Dayyan wrote:

                    Thank you very much. I saw it but it has big volume. In Sari we've just 5kb/s bandwidth. Do you know how can I download it (I can't see it into the online)

                    Ah - I could not see a way to download that video either. Try these smaller ones - I think they can [^] - these you can download and watch offline.

                    'Howard

                    M Offline
                    M Offline
                    Mohammad Dayyan
                    wrote on last edited by
                    #9

                    Thanks again my friend.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups