|
Read this[^], it should hopefully answer your question.
|
|
|
|
|
Thanks, but not quite.
Perhaps I need to explain a little more, the method needs to cope with any list object type. So, if I have a List Object of type string, I need it to return an xml document, but the next List Object I'll next pass it could be an object type and again I need it to return an xml document.
What I am trying to do is have one method to serialize a number List objects of different types to xml in order to handle else where in an application, but of course I do not wish to repeat the code hundred of times.
|
|
|
|
|
Since XML is string based it doesn't matter what you pass in, it will always be a string value in the XML document. What issue are you having difficulty with?
only two letters away from being an asset
|
|
|
|
|
I get you but the approach suggested should work...
I tried these two methods (serializing to file but it could be a memory stream or whatever) and it works
public void Serialize<T>(List<T> list)
{
XmlSerializer xs = new XmlSerializer(typeof(ListHolder<T>));
using (StreamWriter writer = new StreamWriter(@"c:\test.xml"))
{
xs.Serialize(writer, new ListHolder<T>(list));
}
}
public List<T> DeSerialize<T>()
{
List<T> result = null;
XmlSerializer xs = new XmlSerializer(typeof(ListHolder<T>));
using (FileStream fs = new FileStream(@"c:\test.xml", FileMode.Open))
{
XmlReader reader = new XmlTextReader(fs);
ListHolder<T> listHolder = (ListHolder<T>)xs.Deserialize(reader);
fs.Close();
result = listHolder.List;
}
return result;
}
Using this supporting class...
public class ListHolder<T>
{
public ListHolder()
{
list = new List<T>();
}
public ListHolder(List<T> list)
{
this.list = list;
}
private List<T> list;
public List<T> List { get { return list; } }
} To test I used this code:
List<int> toSerialize = new List<int>(3) { 1, 2, 3 };
Serialize<int>(toSerialize);
List<int> deSerialized = DeSerialize<int>(); The xml file is created perfectly and the list is deserialized perfectly from it too.
|
|
|
|
|
I am trying to send an email from Windows Application (C#) through my host email but getting the following error message after a long time waiting:
ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x3b3f18 to COM context 0x3b3da8 for 60 seconds.
The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing
a very long running operation without pumping Windows messages. This situation generally has a negative performance
impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time.
To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives
(such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
here is my code:
string msg = null;
msg = Environment.NewLine + Environment.NewLine;
msg += "TICS User Details" + txtInternetCafe.Text + Environment.NewLine + Environment.NewLine;
msg += "Cafe Name: " + txtInternetCafe.Text + Environment.NewLine + Environment.NewLine;
msg += "Country: " + cboCountry.Text + Environment.NewLine + Environment.NewLine;
msg += "Telephone: " + txtTelephone.Text + Environment.NewLine + Environment.NewLine;
msg += "Email Address: " + txtEmailAddress.Text + Environment.NewLine + Environment.NewLine;
msg += "CPU Serial: " + get_cpu_serial() + Environment.NewLine + Environment.NewLine;
MailAddress mail_from = new MailAddress(txtEmailAddress.Text, txtInternetCafe.Text);
MailAddress mail_to = new MailAddress("EMAIL REMOVED", "JassimRahma.com");
MailMessage mail_message = new MailMessage();
mail_message.From = mail_from;
mail_message.To.Add(mail_to);
mail_message.Subject = "TICS - Registration Request";
mail_message.IsBodyHtml = true;
mail_message.Body = msg;
SmtpClient smtp_client = new SmtpClient("mail.mishkah.org");
NetworkCredential login_info = new NetworkCredential("EMAIL REMOVED", "xxxxx");
smtp_client.UseDefaultCredentials = false;
smtp_client.Credentials = login_info;
smtp_client.Send(mail_message);
mail_message.Dispose();
|
|
|
|
|
|
i did unchecked the Thrown checkbox but now I am getting:
System.Net.WebException: Unable to connect to the remote server---->
System.Net.SocketsException: No Connection could be made because the target machine actively refused it 208.113.200.10:25
what does this mean? and how to fix?
|
|
|
|
|
This is your network connectivity issue. Try to debug and check in which line it throwing the exception.
jrahma wrote: System.Net.SocketsException: No Connection could be made because the target machine actively refused it 208.113.200.10:25
This means, you system is not able to connect with specifed ip.
|
|
|
|
|
Hello Everyone,
I have a very small problem which I cant seem to fix. The following is the code which validates an XML file against a Schema. As the XML file gets parsed, if there is an XMLException in the XML file, the error message indicates the error and outputs the message along with the Line and position. But if there is a XMLSchemaException, it indicates the error but does not tell me the line and position number.
try
{
XmlDocument doc = new XmlDocument();
doc.Load(strXML);
string xmlFrag = doc.InnerXml;
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
reader.ValidationType = ValidationType.Schema;
myschema.Add("http://tempuri.org/dsMinistryH1N1Export.xsd", strXSD);
reader.Schemas.Add(myschema);
while (reader.Read())
{
}
output.Text = ("Completed Validating " + strXML);
}
catch (XmlException XmlExp)
{
output.Text = ("XMLException " + XmlExp.Message);
}
catch (XmlSchemaException XmlSchExp)
{
output.Text = ("XMLSchemaException:" + XmlSchExp.Message);
}
catch (Exception GenExp)
{
output.Text = ("Exception " + GenExp.Message);
}
Note: I have tried using XmlSchExp.LineNumber and XmlSchExp.LinePosition - The problem is I need the user to be able to fix the error in the XML file, not the Schema File. So how do I output the Line/Position of where the error is in the XML file once the reader catches the XML schemaException. I hope I am clear about my question. Thanks in advance for any help.
|
|
|
|
|
Hello everyone...
Im a bit new to c#....
I have written simple program which produces beeps in every 10min i want it to be converted to dll and i wanna use it on my system so that it beeps in every 10min.....
I know it can be easily done with exe file of that project but i wanna use dll and register in system32 and make a part of windows.....
all i know that it's done with csc compiler but what have to be written in /t:library......
ex....
csc /t:library simpledll.cs
it says missing reference or assemblies.....
Thnx....
|
|
|
|
|
You appear to be under the mistake belief that just because you register the .DLL that the code automatically runs. Such is not the case. .DLL's have to be loaded by another process and explicitly execute the code contained within.
You can make this a .DLL all you want - nothing is going to happen. And it does NOT "become part of Windows" when you register something.
You have to leave this as an .EXE, or at least an .EXE that loads and runs the code in the .DLL.
|
|
|
|
|
i know it can't run on own.....i tried to register with regsvr32......................
what is the exact procedure can u please tell...in google im quite messed up....
|
|
|
|
|
No it can't. An exe somewhere has to call the code contained in the dll. This could be a scheduled task, a service or whatever but a dll is incapable of running 'standalone'.
|
|
|
|
|
Hmmmm i got it thnx!....but then why we register dll to system32 using regsvr32 i was in notion that to use as a part of window(i was so wrong!).....
|
|
|
|
|
So an application that uses the objects in that .DLL can instantiate them without knowing the filename and path of the .DLL hosting it. Regsitration only works for COM-based .DLL's. Library and resource .DLL's don't use registration at all.
|
|
|
|
|
cool i got it..in short u meant for reusability to avoid long messy procedures...........
Thnx alot
|
|
|
|
|
Below are the applicable code snippits. Basically from my class poker I update a Global Variable Int from the class GlobalVar. I then want to update the lblwinnings on frmMain from my poker class.
namespace vdp
{
partial public class frmMain: Form
{
public string credits { set { this.lbl.winnings.Text = value; } }
}
class poker
{
public static void Results()
{
frmMain Form1 = new frmMain();
Form1.credits = Convert.ToString(GlobalVar.Winnings);
}
}
}
modified on Wednesday, October 14, 2009 12:26 PM
|
|
|
|
|
This class shouldn't know anything about, nor care about, any Label control on any form. What it should be doing is using a delegate that the parent form can use to get notifications from this class and the form can decide whether or not to update its own label control.
|
|
|
|
|
Thanks, unfortunatley if I understood how to accomplish what you suggested I wouldn't of had to ask the question. I've tried vairous methods, including using a delegate. However I'm still stuck. Examples would much appriciated, thanks so much for your time.
|
|
|
|
|
Here's a quick example of how it could be aproached using an event. An event is a delegate in disguise!
using System;
using System.Windows.Forms;
namespace vdp
{
public partial class FormMain : Form
{
private Poker poker;
private int credits;
public FormMain()
{
InitializeComponent();
poker = new Poker();
labelWinnings.Text = "0";
poker.UpdateWinnings += new EventHandler<WinningsEventArgs>(poker_UpdateWinnings);
DoubleClick += new EventHandler(FormMain_DoubleClick);
}
void FormMain_DoubleClick(object sender, EventArgs e)
{
poker.PlayGame();
}
void poker_UpdateWinnings(object sender, WinningsEventArgs e)
{
AddToCredits(e.Winnings);
}
public void AddToCredits(int winnings)
{
credits += winnings;
labelWinnings.Text = credits.ToString();
}
}
public class Poker
{
public event EventHandler<WinningsEventArgs> UpdateWinnings;
public void PlayGame()
{
OnUpdateWinnings(new WinningsEventArgs(1000));
}
protected virtual void OnUpdateWinnings(WinningsEventArgs e)
{
EventHandler<WinningsEventArgs> eh = UpdateWinnings;
if (eh != null)
eh(this, e);
}
}
public class WinningsEventArgs : EventArgs
{
public WinningsEventArgs(int winnings)
{
Winnings = winnings;
}
public int Winnings
{
get;
private set;
}
}
}
|
|
|
|
|
Thanks, I typed it verbatium into my exisitng code, like before, it compiles and runs fine. But alas the label still doesn't update
|
|
|
|
|
Then you have something wrong with your event raising, subscription or handling. You need to learn/understand how these work to use them successfully.
Here is a brief code snippet with comments which should give you an overview. If this isn't enough, google or search here on CodeProject... there are lots of articles/blogs on it. I even have an article on here covering this subject in some depth - Events Made Simple[^]
public class TestClass
{
private ClassWithEvent classWithEvent;
public TestClass()
{
classWithEvent = new ClassWithEvent();
classWithEvent.Event += new EventHandler(classWithEvent_Event);
}
void classWithEvent_Event(object sender, EventArgs e)
{
Console.WriteLine("Event Rased!");
}
}
public class ClassWithEvent
{
public event EventHandler Event;
protected virtual void OnEvent(EventArgs e)
{
EventHandler eh = Event;
if (eh != null)
eh(this, e);
}
public void PerformEvent()
{
OnEvent(EventArgs.Empty);
}
}
|
|
|
|
|
ok, I got that part to work..I had the following line of code: Poker poker = new Poker(); before I called a method in my poker class from my frmMain class that had to be removed
|
|
|
|
|
Hello,
I was wondering if there is a way to perform a simple left mouse click anywhere on the screen?
Thanks.
|
|
|
|
|
The last time I tried this (about 4 years ago) it was complicated, assuming you want to move the mouse outside the bounds of the executing app. I had to hook the Windows Queue and intercept / process the mouse messages.
There is an article for something similar here
http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx[^]
I'm not sure this will work on OSs newer than XP, or even if this is the best way to go about it. Hopefully one of the opther CPians will have a better suggestion
CCC solved so far: 2 (including a Hard One!)
|
|
|
|