LINQ and XML for compact framework

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Skeet [C# MVP]

    #16
    Re: LINQ and XML for compact framework

    On May 1, 2:21 pm, Wan <luo...@gmail.c omwrote:

    <snip>
    Actual Output has nothing changed. However, if I created a normal
    framework console application project. And copy, paste this code, it
    will do the job. That's why I want to ask how to use LINQ to modify
    XML in compact framework.
    Okay, now we're getting somewhere. I can see three possible reasons to
    start with:

    1) It might not be finding the MessageStaus element
    2) It might be finding the MessageStaus element but failing to change
    the value properly
    3) It might not be able to save the file back to disk
    4) It might be throwing an exception, which you're silently swallowing

    Try putting some kind of alert (e.g. a MessageBox.Show or
    Console.WriteLi ne) in the foreach loop, to check for condition 1.
    Try saving something totally different (e.g. a new XElement) to disk
    to check for condition 3.
    Try putting an alert in your catch block to check for condition 4.

    Jon

    Comment

    • Wan

      #17
      Re: LINQ and XML for compact framework

      On May 1, 2:38 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On May 1, 2:21 pm, Wan <luo...@gmail.c omwrote:
      >
      <snip>
      >
      Actual Output has nothing changed.  However, if I created a normal
      framework console application project. And copy, paste this code, it
      will do the job.  That's why I want to ask how to use LINQ to modify
      XML in compact framework.
      >
      Okay, now we're getting somewhere. I can see three possible reasons to
      start with:
      >
      1) It might not be finding the MessageStaus element
      2) It might be finding the MessageStaus element but failing to change
      the value properly
      3) It might not be able to save the file back to disk
      4) It might be throwing an exception, which you're silently swallowing
      >
      Try putting some kind of alert (e.g. a MessageBox.Show or
      Console.WriteLi ne) in the foreach loop, to check for condition 1.
      Try saving something totally different (e.g. a new XElement) to disk
      to check for condition 3.
      Try putting an alert in your catch block to check for condition 4.
      >
      Jon
      First of all, thanks for your suggestion. I put some alerts as you
      told, I can see that the

      IEnumerable<XEl ementmsgs = from el in root.Descendant s("Message")
      where
      el.Element("Mes sageID").Value == MsgID
      select el;

      foreach (XElement el in msgs)
      {
      el.Element("Mes sageStaus").Set Value(newStatus .ToString());
      }

      'msgs' is empty. There is two possiblities here.
      1) from el in root.Descendant s("Message")
      where el.Element("Mes sageID").Value == MsgID
      select el;
      This LINQ statement is not working.

      2) After the LINQ found the records, it fail to copy the value to
      IEnumerable<XEl ementmsgs

      As I said, I have tested this code and same LINQ statement on a
      desktop console application. It works fine.


      Regards,
      Wan

      Comment

      • Jon Skeet [C# MVP]

        #18
        Re: LINQ and XML for compact framework

        On May 1, 2:54 pm, Wan <luo...@gmail.c omwrote:

        <snip>
        'msgs' is empty. There is two possiblities here.
        1) from el in root.Descendant s("Message")
        where el.Element("Mes sageID").Value == MsgID
        select el;
        This LINQ statement is not working.
        Right. What happens if you remove the where clause? Is it successfully
        finding the messages themselves when unfiltered? That would also
        verify that it's managed to load the document appropriately.
        2) After the LINQ found the records, it fail to copy the value to
        IEnumerable<XEl ementmsgs
        That's just assignment - pretty unlikely to fail.
        As I said, I have tested this code and same LINQ statement on a
        desktop console application. It works fine.
        Sure. The trick is narrowing down where the difference is :)

        Jon

        Comment

        • Wan

          #19
          Re: LINQ and XML for compact framework

          On May 1, 3:08 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On May 1, 2:54 pm, Wan <luo...@gmail.c omwrote:
          >
          <snip>
          >
          'msgs' is empty.  There is two possiblities here.
          1)  from el in root.Descendant s("Message")
               where el.Element("Mes sageID").Value == MsgID
               select el;
              This LINQ statement is not working.
          >
          Right. What happens if you remove the where clause? Is it successfully
          finding the messages themselves when unfiltered? That would also
          verify that it's managed to load the document appropriately.
          >
          2) After the LINQ found the records, it fail to copy the value to
          IEnumerable<XEl ementmsgs
          >
          That's just assignment - pretty unlikely to fail.
          >
          As I said, I have tested this code and same LINQ statement on a
          desktop console application.  It works fine.
          >
          Sure. The trick is narrowing down where the difference is :)
          >
          Jon
          Hi Jon

          Check this out:

          I have got a screent shot here.

          I've removed the where clause, it is still the same.

          Regards,
          Wan

          Comment

          • Jon Skeet [C# MVP]

            #20
            Re: LINQ and XML for compact framework

            On May 1, 3:18 pm, Wan <luo...@gmail.c omwrote:

            <snip>
            Check this out:http://farm3.static.flickr.com/2188/...40ee808b_o.jpg
            I have got a screent shot here.
            That's focusing on the debugger side of things, which I believe is a
            red herring. Concentrate on diagnostics which don't require the
            debugger.
            I've removed the where clause, it is still the same.
            So if you put a call to MessageBox.Show in the foreach loop, having
            removed the where clause, you're still not seeing anything? That's
            worrying.

            When you a call to MessageBox.Show in the exception handler, did it
            show any exceptions being thrown?

            If you put a call to MessageBox.Show after the call to Save(), did it
            show the code getting there successfully?

            Jon

            Comment

            • Wan

              #21
              Re: LINQ and XML for compact framework

              On May 1, 3:40 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
              On May 1, 3:18 pm, Wan <luo...@gmail.c omwrote:
              >
              <snip>
              >
              Check this out:http://farm3.static.flickr.com/2188/...40ee808b_o.jpg
              I have got a screent shot here.
              >
              That's focusing on the debugger side of things, which I believe is a
              red herring. Concentrate on diagnostics which don't require the
              debugger.
              >
              I've removed the where clause, it is still the same.
              >
              So if you put a call to MessageBox.Show in the foreach loop, having
              removed the where clause, you're still not seeing anything? That's
              worrying.
              >
              When you a call to MessageBox.Show in the exception handler, did it
              show any exceptions being thrown?
              >
              If you put a call to MessageBox.Show after the call to Save(), did it
              show the code getting there successfully?
              >
              Jon
              Yes, I still see nothing. And the {"NullReference Exception"} was
              thrown. the Save() statement was not excuted.

              Wan

              Comment

              • Jon Skeet [C# MVP]

                #22
                Re: LINQ and XML for compact framework

                On May 1, 3:59 pm, Wan <luo...@gmail.c omwrote:
                Yes, I still see nothing. And the {"NullReference Exception"} was
                thrown.
                Ah! Now we're getting somewhere! So what was the stack trace? Where
                was the exception being thrown?
                the Save() statement was not excuted.
                No wonder, if an earlier statement was throwing an exception. This is
                why it's a really bad idea to swallow exceptions without logging
                them :)

                Jon

                Comment

                • Wan

                  #23
                  Re: LINQ and XML for compact framework

                  On May 1, 4:16 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                  On May 1, 3:59 pm, Wan <luo...@gmail.c omwrote:
                  >
                  Yes, I still see nothing.  And the {"NullReference Exception"} was
                  thrown.
                  >
                  Ah! Now we're getting somewhere! So what was the stack trace? Where
                  was the exception being thrown?
                  >
                   the Save() statement was not excuted.
                  >
                  No wonder, if an earlier statement was throwing an exception. This is
                  why it's a really bad idea to swallow exceptions without logging
                  them :)
                  >
                  Jon
                  The exception was thrown inside the foreach statement. After it
                  excute
                  el.Element("Mes sageStaus").Set Value(newStatus .ToString());
                  this line of code, the exception was fired.


                  Here is the stack trace:
                  at xmlCFtest.Form1 .updateMsg(Stri ng MsgID, Int32 newStatus)
                  at xmlCFtest.Form1 ..ctor()
                  at xmlCFtest.Progr am.Main()

                  Wan

                  Comment

                  • Jon Skeet [C# MVP]

                    #24
                    Re: LINQ and XML for compact framework

                    On May 1, 4:39 pm, Wan <luo...@gmail.c omwrote:
                    The exception was thrown inside the foreach statement.
                    Hang on - you said before that it wasn't getting inside the foreach
                    statement. You said that "msgs" was empty - which means it wouldn't be
                    trying to set the value.

                    Which is it?
                    After it excute
                    el.Element("Mes sageStaus").Set Value(newStatus .ToString());
                    this line of code, the exception was fired.
                    After it executed that, or during the execution? My guess is that
                    el.Element("Mes sageStaus") is returning null, but you really need to
                    make sure your diagnostics are consistent before you go any further.
                    If it *is* going into the foreach loop, work out why you thought it
                    wasn't.

                    Then look at "el" (again, with a message box or something similar) to
                    see whether it really does have a MessageStaus element.

                    Jon

                    Comment

                    • Wan

                      #25
                      Re: LINQ and XML for compact framework

                      On May 1, 5:41 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                      On May 1, 4:39 pm, Wan <luo...@gmail.c omwrote:
                      >
                      The exception was thrown inside the foreach statement.
                      >
                      Hang on - you said before that it wasn't getting inside the foreach
                      statement. You said that "msgs" was empty - which means it wouldn't be
                      trying to set the value.
                      >
                      Which is it?
                      >
                      After it excute
                      el.Element("Mes sageStaus").Set Value(newStatus .ToString());
                      this line of code, the exception was fired.
                      >
                      After it executed that, or during the execution? My guess is that
                      el.Element("Mes sageStaus") is returning null, but you really need to
                      make sure your diagnostics are consistent before you go any further.
                      If it *is* going into the foreach loop, work out why you thought it
                      wasn't.
                      >
                      Then look at "el" (again, with a message box or something similar) to
                      see whether it really does have a MessageStaus element.
                      >
                      Jon
                      Hi Jon
                      Problem is solved. The LINQ statement I wrote was incorrect. Because
                      in my XML, I have namespace, but I didn't specify it in my XElement.

                      Thanks very much for your help :-)

                      Best Regards,
                      Wan

                      Comment

                      Working...