471,338 Members | 1,082 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

LINQ and XML for compact framework

Hi

I am currently building an application on .NetCF 3.5 in C#. The
application needs to process xml files. I am thinking about using LINQ
to process XML. I wrote a piece of code to modify the specified
element's value.
XmlReaderSettings settings = new
XmlReaderSettings();
settings.ConformanceLevel =
ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(inboxPath,
settings);

XElement root = XElement.Load(reader);
IEnumerable<XElementmsgs = from el in
root.Descendants("Message")
where el.Element("MessageID").Value ==
MsgID
select el;

foreach (XElement el in msgs)
{

el.Element("MessageStaus").SetValue(newStatus.ToSt ring());
}
root.Save(inboxPath);

Here is the error message that I received :

msgs.System.Collections.Generic.IEnumerator<TSourc e>.Current 'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
does not contain a definition for 'System' and no extension method
'System' accepting a first argument of type
'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
could be found (are you missing a using directive or an assembly
reference?)

Could someone please explain to me why I get such error message? By
the way I have refference all the necessary dlls. (System.linq;
System.xml.linq)

Is it because I am using compact framework? I've tested this code on
a normal framework. It is working fine.

Jun 27 '08 #1
24 3684
<lu****@gmail.comwrote:

<snip>
Is it because I am using compact framework? I've tested this code on
a normal framework. It is working fine.
That's very strange indeed - what line does the error message point at?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
Wan
On Apr 30, 7:52*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
*<luo...@gmail.comwrote:

<snip>
Is it because I am using compact framework? *I've tested this code on
a normal framework. *It is working fine.

That's very strange indeed - what line does the error message point at?

Could you post a short but complete program which demonstrates the
problem?

Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
what I mean by that.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Actually it is not error, my program is still running. However, if I
place a break point at
IEnumerable<XElementmsgs = from el in
root.Descendants("Message")
where el.Element("MessageID").Value ==
MsgID
select el;

After the statement is excuted, if I look up 'msgs' 's value, I will
get such error message.
Jun 27 '08 #3
Wan <lu****@gmail.comwrote:
Actually it is not error, my program is still running. However, if I
place a break point at
IEnumerable<XElementmsgs = from el in
root.Descendants("Message")
where el.Element("MessageID").Value ==
MsgID
select el;
After the statement is excuted, if I look up 'msgs' 's value, I will
get such error message.
In that case it sounds like a debugger problem rather than anything
else.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4
Wan
On Apr 30, 9:46*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Wan <luo...@gmail.comwrote:
Actually it is not error, my program is still running. *However, if I
place a break point at
IEnumerable<XElementmsgs = from el in
root.Descendants("Message")
* * * * * * * * * * * * * * * *where el.Element("MessageID").Value ==
MsgID
* * * * * * * * * * * * * * * *select el;
After the statement is excuted, if I look up 'msgs' 's value, I will
get such error message.

In that case it sounds like a debugger problem rather than anything
else.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
I've tested on two PCs, both the same.
Can some one post a tested working code of query the XML by using LINQ
with compact framework?
Jun 27 '08 #5
Wan <lu****@gmail.comwrote:
I've tested on two PCs, both the same.
Can some one post a tested working code of query the XML by using LINQ
with compact framework?
Well hang on - you've posted an error which you look in the debugger.
You've said the program is still running. What's it doing that you
don't expect it to?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #6
Wan
On Apr 30, 10:14*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Wan <luo...@gmail.comwrote:
I've tested on two PCs, both the same.
Can some one post a tested working code of query the XML by using LINQ
with compact framework?

Well hang on - you've posted an error which you look in the debugger.
You've said the program is still running. What's it doing that you
don't expect it to?

Could you post a short but complete program which demonstrates the
problem?

Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
what I mean by that.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Here is the full sample code. with the XML sample.


Code Snippetusing System;

using System.Linq;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml.Linq;

using System.Xml;

namespace xmlCFtest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

updateMsg("14722", 5);

}

private void updateMsg(string MsgID, int newStatus)

{

try

{

XmlReaderSettings settings = new XmlReaderSettings();

settings.ConformanceLevel = ConformanceLevel.Fragment;

settings.IgnoreWhitespace = true;

settings.IgnoreComments = true;

XmlReader reader = XmlReader.Create(@"Program Files\messageclient\XML
\Inbox.xml", settings);

XElement root = XElement.Load(reader);

IEnumerable<XElementmsgs = from el in root.Descendants("Message")

where el.Element("MessageID").Value == MsgID

select el;

foreach (XElement el in msgs)

{

el.Element("MessageStaus").SetValue(newStatus.ToSt ring());

}

root.Save(@"Program Files\messageclient\XML\Inbox.xml");

}

catch (Exception e) { }

}

}

}



XML Sample:


Code Snippet<?xml version="1.0"?>
<ArrayOfMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Message>
<IMEI xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">35340401169569600</IMEI>
<MessageText xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">hi</MessageText>
<MessageID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">14722</MessageID>
<UserID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">12984</UserID>
<SenderName xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">HardCode Test </SenderName>
<TimeStamp xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">2008-04-29T16:26:36.097</TimeStamp>
<MessageStaus xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">3</MessageStaus>
</Message>

</ArrayOfMessage>
Hi Jon
When I said the program was running, I mean the code was compiled.
However, the code does not do the job. The project I created was
based on Windows Mobile 6 SDK and Compact Framework 3.5.

Regards,
Wan
Jun 27 '08 #7
On May 1, 10:56 am, Wan <luo...@gmail.comwrote:
Here is the full sample code. with the XML sample.
Well, it's not exactly a full sample - it doesn't contain a Main
method, for instance. It's not even a complete class - where's the
implementation of InitializeComponent?
Hint: console apps are generally a lot shorter.

See http://pobox.com/~skeet/csharp/incomplete.html
When I said the program was running, I mean the code was compiled.
However, the code does not do the job. The project I created was
based on Windows Mobile 6 SDK and Compact Framework 3.5.
When you say "the code does not do the job" what exactly do you mean?
What does it do, and what do you want it to do?

Jon
Jun 27 '08 #8
Wan
On May 1, 11:44*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 10:56 am, Wan <luo...@gmail.comwrote:
Here is the full sample code. with the XML sample.

Well, it's not exactly a full sample - it doesn't contain a Main
method, for instance. It's not even a complete class - where's the
implementation of InitializeComponent?
Hint: console apps are generally a lot shorter.

Seehttp://pobox.com/~skeet/csharp/incomplete.html
When I said the program was running, I mean the code was compiled.
However, the code does not do the job. *The project I created was
based on Windows Mobile 6 SDK and Compact Framework 3.5.

When you say "the code does not do the job" what exactly do you mean?
What does it do, and what do you want it to do?

Jon
Hi Jon

I think it is complete, Please create a windows application project
with smart device. Put the code under Form.cs. I don't think you
need to implement a 'Main'.

Regards,
Wan
Jun 27 '08 #9
Wan
On May 1, 11:44*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 10:56 am, Wan <luo...@gmail.comwrote:
Here is the full sample code. with the XML sample.

Well, it's not exactly a full sample - it doesn't contain a Main
method, for instance. It's not even a complete class - where's the
implementation of InitializeComponent?
Hint: console apps are generally a lot shorter.

Seehttp://pobox.com/~skeet/csharp/incomplete.html
When I said the program was running, I mean the code was compiled.
However, the code does not do the job. *The project I created was
based on Windows Mobile 6 SDK and Compact Framework 3.5.

When you say "the code does not do the job" what exactly do you mean?
What does it do, and what do you want it to do?

Jon
'InitializeComponent' method is generated when the project is
created. Since VS 2005, it is seperated from the form class.

Regards,
Wan
Jun 27 '08 #10
On May 1, 12:43 pm, Wan <luo...@gmail.comwrote:
I think it is complete,
No, it's definitely not.
Please create a windows application project
with smart device. Put the code under Form.cs. I don't think you
need to implement a 'Main'.
Only because Visual Studio creates another file for you that *does*
contain Main. Likewise the Form1.Designer.cs file contains
InitializeComponent. Without that, it's not complete.

In short, I can't cut and paste the code into a text editor *on its
own* and compile from the command line, which is what I tend to do
with newsgroup posts. Again, please read
http://pobox.com/~skeet/csharp/incomplete.html

You also *still* haven't said how it behaves differently to how you
expect it to.

Jon
Jun 27 '08 #11
Wan
On May 1, 1:07*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 12:43 pm, Wan <luo...@gmail.comwrote:
I think it is complete,

No, it's definitely not.
Please create a windows application project
with smart device. *Put the code under Form.cs. *I don't think you
need to implement a 'Main'.

Only because Visual Studio creates another file for you that *does*
contain Main. Likewise the Form1.Designer.cs file contains
InitializeComponent. Without that, it's not complete.

In short, I can't cut and paste the code into a text editor *on its
own* and compile from the command line, which is what I tend to do
with newsgroup posts. Again, please readhttp://pobox.com/~skeet/csharp/incomplete.html

You also *still* haven't said how it behaves differently to how you
expect it to.

Jon
Ok, I am not sure if you complie it from command line will help to see
the problem. As the program was complied with no errors.

I expect this piece of code can go to the XML I pasted, and find the
element as well as changing the value. However, it didn't changed
value. even though it found the XML file, loaded XML, ran the LINQ
statement. Finally when I check the value of "IEnumerable<XElement>
msgs" by watching this variable. It will give me such error message:
msgs.System.Collections.Generic.IEnumerator<TSourc e>.Current
'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
does not contain a definition for 'System' and no extension method
'System' accepting a first argument of type
'System.Collections.Generic.IEnumerable<System.Xml .Linq.XElement>'
could be found (are you missing a using directive or an assembly
reference?)

I assume, it might be problem of 'System.Collections.Generic' for
compact framework, does not support the XElement type.

Jun 27 '08 #12
Wan
On May 1, 1:07*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 12:43 pm, Wan <luo...@gmail.comwrote:
I think it is complete,

No, it's definitely not.
Please create a windows application project
with smart device. *Put the code under Form.cs. *I don't think you
need to implement a 'Main'.

Only because Visual Studio creates another file for you that *does*
contain Main. Likewise the Form1.Designer.cs file contains
InitializeComponent. Without that, it's not complete.

In short, I can't cut and paste the code into a text editor *on its
own* and compile from the command line, which is what I tend to do
with newsgroup posts. Again, please readhttp://pobox.com/~skeet/csharp/incomplete.html

You also *still* haven't said how it behaves differently to how you
expect it to.

Jon
The Program Has three files

1. Program.cs
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace xmlCFtest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
2. Form1.cs
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;

namespace xmlCFtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
updateMsg("14722", 5);
}

private void updateMsg(string MsgID, int newStatus)
{
try
{
lock ("Hello")
{
XmlReaderSettings settings = new
XmlReaderSettings();
settings.ConformanceLevel =
ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(@"Program Files
\messageclient\XML\Inbox.xml", settings);

XElement root = XElement.Load(reader);
IEnumerable<XElementmsgs = from el in
root.Descendants("Message")
where
el.Element("MessageID").Value == MsgID
select el;

foreach (XElement el in msgs)
{

el.Element("MessageStaus").SetValue(newStatus.ToSt ring());
}

root.Save(@"Program Files\messageclient\XML
\Inbox.xml");
}
}
catch (Exception e) { }

}
}
}
3. Form1.Designer.cs
namespace xmlCFtest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.MainMenu mainMenu1;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.components = new System.ComponentModel.Container();
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Dpi;
this.Text = "Form1";
this.AutoScroll = true;
}

#endregion
}
}

Regards,
Wan
Jun 27 '08 #13
On May 1, 1:27 pm, Wan <luo...@gmail.comwrote:
Ok, I am not sure if you complie it from command line will help to see
the problem.
It would if you would tell us how the program behaved differently to
how you expected - without referring to the debugger.
As the program was complied with no errors.
Yes, many short but complete programs do.
I expect this piece of code can go to the XML I pasted, and find the
element as well as changing the value. However, it didn't changed
value. even though it found the XML file, loaded XML, ran the LINQ
statement. Finally when I check the value of "IEnumerable<XElement>
msgs" by watching this variable. It will give me such error message:
Yes, so you've said. That's in the debugger. Now, can you show a
program which produces output X when you expect output Y, without
having to break and look at the debugger? If it's *only* an issue with
the debugger, I'm unlikely to be able to help. If the program isn't
working as expected however, then please specify in what way that is
the case.

Here's an example of what I mean:

<sample post>
Problem: Addition with large numbers appears not to work.

I'm having trouble with addition when it comes to large integers

Program:
using System;

class Test
{
static void Main()
{
int i = 2147483647;
Console.WriteLine("i={0}", i);
Console.WriteLine("i+1={0}", i+1);
}
}

Expected output:
i=2147483647
i+1=2147483648

Actual output:
i=2147483647
i+1=-2147483648

Note that the result is negative. Why?
</sample post>

At this point:
a) I can easily compile and run the code
b) I can understand the problem
c I can verify that I see the problem too
d) I can easily change the code to try to fix it

Now obviously that's a trivial example - but it's a good model. While
you can only express what's wrong in terms of watching a variable in
the debugger, I'm inclined to put the issue down to a debugger
problem.

Jon
Jun 27 '08 #14
Wan
On May 1, 2:09*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 1:27 pm, Wan <luo...@gmail.comwrote:
Ok, I am not sure if you complie it from command line will help to see
the problem.

It would if you would tell us how the program behaved differently to
how you expected - without referring to the debugger.
*As the program was complied with no errors.

Yes, many short but complete programs do.
I expect this piece of code can go to the XML I pasted, and find the
element as well as changing the value. *However, it didn't changed
value. *even though it found the XML file, loaded XML, ran the LINQ
statement. *Finally when I check the value of "IEnumerable<XElement>
msgs" by watching this variable. *It will give me such error message:

Yes, so you've said. That's in the debugger. Now, can you show a
program which produces output X when you expect output Y, without
having to break and look at the debugger? If it's *only* an issue with
the debugger, I'm unlikely to be able to help. If the program isn't
working as expected however, then please specify in what way that is
the case.

Here's an example of what I mean:

<sample post>
Problem: Addition with large numbers appears not to work.

I'm having trouble with addition when it comes to large integers

Program:
using System;

class Test
{
* * static void Main()
* * {
* * * * int i = 2147483647;
* * * * Console.WriteLine("i={0}", i);
* * * * Console.WriteLine("i+1={0}", i+1);
* * }

}

Expected output:
i=2147483647
i+1=2147483648

Actual output:
i=2147483647
i+1=-2147483648

Note that the result is negative. Why?
</sample post>

At this point:
a) I can easily compile and run the code
b) I can understand the problem
c I can verify that I see the problem too
d) I can easily change the code to try to fix it

Now obviously that's a trivial example - but it's a good model. While
you can only express what's wrong in terms of watching a variable in
the debugger, I'm inclined to put the issue down to a debugger
problem.

Jon
The original XML file:

<?xml version="1.0"?>
<ArrayOfMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Message>
<IMEI xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">35340401169569600</IMEI>
<MessageText xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">hi</MessageText>
<MessageID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">14722</MessageID>
<UserID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">12984</UserID>
<SenderName xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">HardCode Test </SenderName>
<TimeStamp xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">2008-04-29T16:26:36.097</TimeStamp>
<MessageStaus xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">3</MessageStaus>
</Message>
</ArrayOfMessage>

After run the program I wrote, I expect the XML to become:
<?xml version="1.0"?>
<ArrayOfMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Message>
<IMEI xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">35340401169569600</IMEI>
<MessageText xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">hi</MessageText>
<MessageID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">14722</MessageID>
<UserID xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">12984</UserID>
<SenderName xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">HardCode Test </SenderName>
<TimeStamp xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">2008-04-29T16:26:36.097</TimeStamp>
<MessageStaus xmlns="http://FleetMatics.com/WCFMessageService/
Message.xsd">5</MessageStaus>
</Message>
</ArrayOfMessage>

The last element <MessageStaus></MessageStausshould change its
value.

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.
Wan

Jun 27 '08 #15
On May 1, 2:21 pm, Wan <luo...@gmail.comwrote:

<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.WriteLine) 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
Jun 27 '08 #16
Wan
On May 1, 2:38*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 2:21 pm, Wan <luo...@gmail.comwrote:

<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.WriteLine) 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<XElementmsgs = from el in root.Descendants("Message")
where
el.Element("MessageID").Value == MsgID
select el;

foreach (XElement el in msgs)
{
el.Element("MessageStaus").SetValue(newStatus.ToSt ring());
}

'msgs' is empty. There is two possiblities here.
1) from el in root.Descendants("Message")
where el.Element("MessageID").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<XElementmsgs

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

Jun 27 '08 #17
On May 1, 2:54 pm, Wan <luo...@gmail.comwrote:

<snip>
'msgs' is empty. There is two possiblities here.
1) from el in root.Descendants("Message")
where el.Element("MessageID").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<XElementmsgs
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
Jun 27 '08 #18
Wan
On May 1, 3:08*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 2:54 pm, Wan <luo...@gmail.comwrote:

<snip>
'msgs' is empty. *There is two possiblities here.
1) *from el in root.Descendants("Message")
* * *where el.Element("MessageID").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<XElementmsgs

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:
http://farm3.static.flickr.com/2188/...40ee808b_o.jpg
I have got a screent shot here.

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

Regards,
Wan
Jun 27 '08 #19
On May 1, 3:18 pm, Wan <luo...@gmail.comwrote:

<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
Jun 27 '08 #20
Wan
On May 1, 3:40*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 3:18 pm, Wan <luo...@gmail.comwrote:

<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 {"NullReferenceException"} was
thrown. the Save() statement was not excuted.

Wan
Jun 27 '08 #21
On May 1, 3:59 pm, Wan <luo...@gmail.comwrote:
Yes, I still see nothing. And the {"NullReferenceException"} 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
Jun 27 '08 #22
Wan
On May 1, 4:16*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 3:59 pm, Wan <luo...@gmail.comwrote:
Yes, I still see nothing. *And the {"NullReferenceException"} 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("MessageStaus").SetValue(newStatus.ToSt ring());
this line of code, the exception was fired.
Here is the stack trace:
at xmlCFtest.Form1.updateMsg(String MsgID, Int32 newStatus)
at xmlCFtest.Form1..ctor()
at xmlCFtest.Program.Main()

Wan

Jun 27 '08 #23
On May 1, 4:39 pm, Wan <luo...@gmail.comwrote:
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("MessageStaus").SetValue(newStatus.ToSt ring());
this line of code, the exception was fired.
After it executed that, or during the execution? My guess is that
el.Element("MessageStaus") 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
Jun 27 '08 #24
Wan
On May 1, 5:41*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 1, 4:39 pm, Wan <luo...@gmail.comwrote:
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("MessageStaus").SetValue(newStatus.ToSt ring());
this line of code, the exception was fired.

After it executed that, or during the execution? My guess is that
el.Element("MessageStaus") 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
Jun 27 '08 #25

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Earle Oxner | last post: by
1 post views Thread by Asad Khan | last post: by
reply views Thread by Roland Rosier | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.