How to query on a xml document like SQL?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AliRezaGoogle

    How to query on a xml document like SQL?

    Dear Members,
    I want to find a collection of nodes with certain attributes.
    For example I want to retrieve every books with name = “NLP of MT”
    from this XML document:

    <inventory>

    <books>
    <book Name = “CS”</book>
    <book Name = “NLP of MT”</book>
    <book Name = “MT”</book>
    </books>

    </inventory>

    This can be easily expressed in SQL like:
    “ SELECT * FROM INVENTROY.BOOKS WHERE NAME = ‘NLP of MT’ “

    How can I do this with XPath? Other ways are highly welcomed.
    Please provide me a clear snippet of code.

    Many thanks in advance,
  • =?windows-1252?Q?Arne_Vajh=F8j?=

    #2
    Re: How to query on a xml document like SQL?

    AliRezaGoogle wrote:
    I want to find a collection of nodes with certain attributes.
    For example I want to retrieve every books with name = “NLP of MT”
    from this XML document:
    >
    <inventory>
    >
    <books>
    <book Name = “CS”</book>
    <book Name = “NLP of MT”</book>
    <book Name = “MT”</book>
    </books>
    >
    </inventory>
    >
    This can be easily expressed in SQL like:
    “ SELECT * FROM INVENTROY.BOOKS WHERE NAME = ‘NLP of MT’ “
    >
    How can I do this with XPath? Other ways are highly welcomed.
    The XPath expression "//inventory/books/book[@Name='NLP of MT']" should
    do it.

    Alternatively LINQ for XML could be an option.

    Arne

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: How to query on a xml document like SQL?

      AliRezaGoogle <asemoonya@yaho o.comwrote:
      Dear Members,
      I want to find a collection of nodes with certain attributes.
      For example I want to retrieve every books with name = =3FNLP of MT=3F
      from this XML document:
      >
      <inventory>
      >
      <books>
      <book Name = =3FCS=3F</book>
      <book Name = =3FNLP of MT=3F</book>
      <book Name = =3FMT=3F</book>
      </books>
      >
      </inventory>
      >
      This can be easily expressed in SQL like:
      =3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F
      >
      How can I do this with XPath? Other ways are highly welcomed.
      Please provide me a clear snippet of code.
      Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
      the most readable query syntax (IMO). Otherwise, XPath is the way to
      go. If you could give us more details of this, we can code up an
      example.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • AliRezaGoogle

        #4
        Re: How to query on a xml document like SQL?

        On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        AliRezaGoogle <asemoo...@yaho o.comwrote:
        Dear Members,
        I want to find a collection of nodes with certain attributes.
        For example I want to retrieve every books with name = =3FNLP of MT=3F
        from this XML document:
        >
        <inventory>
        >
        <books>
        <book Name = =3FCS=3F</book>
        <book Name = =3FNLP of MT=3F</book>
        <book Name = =3FMT=3F</book>
        </books>
        >
        </inventory>
        >
        This can be easily expressed in SQL like:
        =3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F
        >
        How can I do this with XPath? Other ways are highly welcomed.
        Please provide me a clear snippet of code.
        >
        Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
        the most readable query syntax (IMO). Otherwise, XPath is the way to
        go. If you could give us more details of this, we can code up an
        example.
        >
        --
        Jon Skeet - <sk...@pobox.co m>
        Web site:http://www.pobox.com/~skeet
        Blog:http://www.msmvps.com/jon.skeet
        C# in Depth:http://csharpindepth.com
        No. I can not use what you mentioned. I use .Net Framework 2 with
        VS2005

        Comment

        • Jeff Winn

          #5
          Re: How to query on a xml document like SQL?

          Well then you'll need to use XPath as Arne suggested

          "AliRezaGoo gle" <asemoonya@yaho o.comwrote in message
          news:7fa028e2-5d83-48bd-b29a-f42eeccd0b4a@k3 0g2000hse.googl egroups.com...
          On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
          >AliRezaGoogl e <asemoo...@yaho o.comwrote:
          Dear Members,
          I want to find a collection of nodes with certain attributes.
          For example I want to retrieve every books with name = =3FNLP of MT=3F
          from this XML document:
          >>
          <inventory>
          >>
          <books>
          <book Name = =3FCS=3F</book>
          <book Name = =3FNLP of MT=3F</book>
          <book Name = =3FMT=3F</book>
          </books>
          >>
          </inventory>
          >>
          This can be easily expressed in SQL like:
          =3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F
          =3F
          >>
          How can I do this with XPath? Other ways are highly welcomed.
          Please provide me a clear snippet of code.
          >>
          >Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
          >the most readable query syntax (IMO). Otherwise, XPath is the way to
          >go. If you could give us more details of this, we can code up an
          >example.
          >>
          >--
          >Jon Skeet - <sk...@pobox.co m>
          >Web site:http://www.pobox.com/~skeet
          >Blog:http://www.msmvps.com/jon.skeet
          >C# in Depth:http://csharpindepth.com
          >
          No. I can not use what you mentioned. I use .Net Framework 2 with
          VS2005

          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: How to query on a xml document like SQL?

            AliRezaGoogle wrote:
            On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
            >AliRezaGoogl e <asemoo...@yaho o.comwrote:
            >>Dear Members,
            >>I want to find a collection of nodes with certain attributes.
            >>For example I want to retrieve every books with name = =3FNLP of MT=3F
            >>from this XML document:
            >><inventory>
            >> <books>
            >> <book Name = =3FCS=3F</book>
            >> <book Name = =3FNLP of MT=3F</book>
            >> <book Name = =3FMT=3F</book>
            >> </books>
            >></inventory>
            >>This can be easily expressed in SQL like:
            >>=3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F
            >>How can I do this with XPath? Other ways are highly welcomed.
            >>Please provide me a clear snippet of code.
            >Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
            >the most readable query syntax (IMO). Otherwise, XPath is the way to
            >go. If you could give us more details of this, we can code up an
            >example.
            >
            No. I can not use what you mentioned. I use .Net Framework 2 with
            VS2005
            Then use XPath.

            Arne

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: How to query on a xml document like SQL?

              AliRezaGoogle <asemoonya@yaho o.comwrote:

              <snip>
              No. I can not use what you mentioned. I use .Net Framework 2 with
              VS2005
              Okay, so you want XPath, probably. Here's a short but complete program
              to demonstrate:

              using System;
              using System.Xml;

              public class Test
              {
              const string SampleXml = @"
              <inventory>
              <books>
              <book Name = 'CS'>First content</book>
              <book Name = 'NLP of MT'>Second content</book>
              <book Name = 'MT'>Third content</book>
              </books>
              </inventory>";

              static void Main()
              {
              XmlDocument doc = new XmlDocument();
              // You'd normally load from a file here, or
              // something like that
              doc.LoadXml(Sam pleXml);


              XmlNode node = doc.SelectSingl eNode
              ("/inventory/books/book[@Name='NLP of MT']");

              Console.WriteLi ne("Found: {0}", node.InnerText) ;
              }
              }

              Note that if you receive the name dynamically and it could be arbitrary
              text, you end up with a trickier problem due to quoting etc. At that
              point it may be better to programmaticall y look through all the book
              elements. (There may be a way of effectively parameterising XPath
              expressions, but I don't know it...)

              --
              Jon Skeet - <skeet@pobox.co m>
              Web site: http://www.pobox.com/~skeet
              Blog: http://www.msmvps.com/jon.skeet
              C# in Depth: http://csharpindepth.com

              Comment

              Working...