8 Replies - 822 Views - Last Post: 18 January 2022 - 06:07 AM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 396
  • Joined: 13-March 12

XML Reading

Posted 13 January 2022 - 07:52 AM

I am wanting all <result-entry> elements where the succeed attribute = "no." So I wrote this query;

     Public Function GetBadResults(doc As XDocument) As IEnumerable(Of XElement)
        Dim errList As IEnumerable(Of XElement) =
            From el In doc.Elements()
            Where [email protected] = "No"
            Select el

        Return errList

    End Function



This query returns nothing, an
Here is the XDocument
<?xml version="1.0" encoding="utf-16"?>
<response>
  <result-response>
    <load entity="TEST" id="4748746" mode="entry">
      <result-entry result-id="55476977" succeed="Yes">
        <return FORMATTED_RESULT="01/12/2022 12:16:56" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526604" succeed="Yes">
        <return FORMATTED_RESULT="46" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526605" succeed="Yes">
        <return FORMATTED_RESULT="A" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526606" succeed="Yes">
        <return FORMATTED_RESULT="A" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526607" succeed="No">
        <return FORMATTED_RESULT="1529" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526608" succeed="Yes">
        <return FORMATTED_RESULT="1470" STATUS="A" />
      </result-entry>
      <result-entry result-id="55526609" succeed="No">
        <return FORMATTED_RESULT="53.9" STATUS="A" />
      </result-entry>
      <result-entry result-id="55526610" succeed="Yes">
        <return FORMATTED_RESULT="15.5" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526611" succeed="Yes">
        <return FORMATTED_RESULT="1577" STATUS="C" />
      </result-entry>
      <result-entry result-id="55526612" succeed="No">
        <return FORMATTED_RESULT="1.17284080571728" STATUS="A" />
      </result-entry>
      <result-entry result-id="55526613" succeed="Yes">
        <return FORMATTED_RESULT="15" STATUS="C" />
      </result-entry>
    </load>
  </result-response>
</response>



I have not been able to get a grip on XML file operations yet! I think I have a mental block. I might need surgery. Can anyone tell me what is wrong with the query?

Is This A Good Question/Topic? 0
  • +

Replies To: XML Reading

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16503
  • View blog
  • Posts: 65,458
  • Joined: 12-June 08

Re: XML Reading

Posted 13 January 2022 - 08:30 AM

You would want to always start with the collection of nodes from the root - hence the 'Descendants()'.

From there you would have LINQ get the attribute by a specific name, and then search by that value.

            var foo = (from x in xdoc.Descendants()
                      where (String) x.Attribute("succeed") == "No"
                      select x).ToList();


Was This Post Helpful? 0
  • +
  • -

#3 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 396
  • Joined: 13-March 12

Re: XML Reading

Posted 13 January 2022 - 11:00 AM

Thanks for your response. OK, x is an element object in xDoc. So this should get all elements whose success attribute ="No." Seems clear enough, but it is not working.
   Public Function GetBadResults(doc As XDocument) As IEnumerable(Of XElement)
        Dim errList As IEnumerable(Of XElement) =
            From el In doc.Descendants()
            Where el.Attribute("success") = "No"
            Select el

        Return errList

    End Function




This still returns nothing. "Enumeration yielded no results."
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16503
  • View blog
  • Posts: 65,458
  • Joined: 12-June 08

Re: XML Reading

Posted 13 January 2022 - 11:37 AM

Your example says 'succeed' not 'success'.

17      <result-entry result-id="55526607" succeed="No">
18        <return FORMATTED_RESULT="1529" STATUS="C" />


Was This Post Helpful? 1
  • +
  • -

#5 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 396
  • Joined: 13-March 12

Re: XML Reading

Posted 14 January 2022 - 07:05 AM

Oh man! What a stupid mistake! But the correction didn't work either.
Was This Post Helpful? 0
  • +
  • -

#6 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 127
  • View blog
  • Posts: 700
  • Joined: 01-October 08

Re: XML Reading

Posted 14 January 2022 - 07:15 AM

Your LINQ was wrong, it needed to be looking at result-entry,

    Public Function GetBadResults(doc As XDocument) As IEnumerable(Of XElement)
        Dim errList As IEnumerable(Of XElement) =
            From el In doc.Elements...<result-entry>
            Where [email protected] = "No"
            Select el

        Return errList

    End Function


Was This Post Helpful? 1
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16503
  • View blog
  • Posts: 65,458
  • Joined: 12-June 08

Re: XML Reading

Posted 14 January 2022 - 07:59 AM

Weird.. it works for me given the file definition in the first post.
        Dim filename As String = "c:\test\20220113_xml.txt"
        Dim doc As XDocument
        Using streamReader = New StreamReader(filename, True)
            doc = Xdocument.Load(streamReader)
        End Using

        ' As IEnumerable(Of XElement)
        Dim errList As IEnumerable(Of XElement) = From el In doc.Descendants()
                                                  Where el.Attribute("succeed") = "No"
                                                  Select el

        Console.WriteLine(errList.Count) '3


Was This Post Helpful? 0
  • +
  • -

#8 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 127
  • View blog
  • Posts: 700
  • Joined: 01-October 08

Re: XML Reading

Posted 14 January 2022 - 10:52 AM

View Postmodi123_1, on 14 January 2022 - 09:59 AM, said:

Weird.. it works for me given the file definition in the first post.
        Dim filename As String = "c:\test\20220113_xml.txt"
        Dim doc As XDocument
        Using streamReader = New StreamReader(filename, True)
            doc = Xdocument.Load(streamReader)
        End Using

        ' As IEnumerable(Of XElement)
        Dim errList As IEnumerable(Of XElement) = From el In doc.Descendants()
                                                  Where el.Attribute("succeed") = "No"
                                                  Select el

        Console.WriteLine(errList.Count) '3



The difference is the number of nodes being looked at. You are looking at ALL of the elements. Any element with a succeed attribute = "No" would be selected.

edit: I timed both versions and yours seems faster.

This post has been edited by dbasnett: 18 January 2022 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 396
  • Joined: 13-March 12

Re: XML Reading

Posted 18 January 2022 - 06:07 AM

Thanks to both of you! I don't know what I did differently than last week, but it works fine with both versions. I appreciate the time you spent helping me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1