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. C#
  4. Query XML with LINQ

Query XML with LINQ

Scheduled Pinned Locked Moved C#
csharpdatabaselinqcomxml
6 Posts 2 Posters 0 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.
  • T Offline
    T Offline
    treuveni
    wrote on last edited by
    #1

    Hi, I've the following XML-

    <MessageParts xmlns="http://www.address.com">
    <Status xmlns="">
    .
    .
    .
    .
    <Data>
    <PersonsMin xmlns="some attribute">
    .
    .
    .
    <Parts>
    .
    .
    .
    </Parts>
    </PersonsMin>
    </Data>
    </Status>
    </MessageParts>

    I'me trying to get all the elements in PersonsMin. var query = from response in xmlDoc.Elements("PersonsMin") select response; But i'm getting "Sequence contains no elements". What i'm doing wrong?

    P 1 Reply Last reply
    0
    • T treuveni

      Hi, I've the following XML-

      <MessageParts xmlns="http://www.address.com">
      <Status xmlns="">
      .
      .
      .
      .
      <Data>
      <PersonsMin xmlns="some attribute">
      .
      .
      .
      <Parts>
      .
      .
      .
      </Parts>
      </PersonsMin>
      </Data>
      </Status>
      </MessageParts>

      I'me trying to get all the elements in PersonsMin. var query = from response in xmlDoc.Elements("PersonsMin") select response; But i'm getting "Sequence contains no elements". What i'm doing wrong?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      treuveni wrote:

      What i'm doing wrong?

      That's fairly simple, you've forgotten to add the XML namespace to the query. Typically you would do this using

      XNamespace myNamespace = "some namespace";
      var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin")
      select response;

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      T 1 Reply Last reply
      0
      • P Pete OHanlon

        treuveni wrote:

        What i'm doing wrong?

        That's fairly simple, you've forgotten to add the XML namespace to the query. Typically you would do this using

        XNamespace myNamespace = "some namespace";
        var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin")
        select response;

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        T Offline
        T Offline
        treuveni
        wrote on last edited by
        #3

        Pete O'Hanlon wrote:

        you've forgotten to add the XML namespace to the query

        Like this? XNamespace myNamespace = "some attribute"; var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin") select response; XNamespace is the XAttribute?

        P 1 Reply Last reply
        0
        • T treuveni

          Pete O'Hanlon wrote:

          you've forgotten to add the XML namespace to the query

          Like this? XNamespace myNamespace = "some attribute"; var query = from response in xmlDoc.Elements(myNamespace + "PersonsMin") select response; XNamespace is the XAttribute?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          T 1 Reply Last reply
          0
          • P Pete OHanlon

            Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            T Offline
            T Offline
            treuveni
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.

            Still same error "Sequence contains no elements"

            P 1 Reply Last reply
            0
            • T treuveni

              Pete O'Hanlon wrote:

              Whereever you see xmlns in your XML, that's the namespace that you need to include to pick it up.

              Still same error "Sequence contains no elements"

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Try this:

              var query = from response in xDoc.Descendants(ns + "PersonsMin")
              select response;

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              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