|
Hi Keefb,
Thanks for the response and the input.
keefb wrote: You will be better off creating the controller element in the foreach (var controller in ControllerList) loop, adding it to the building.
Makes sense ... thanks, lets see what I can do
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
No probs. You should also find that, by creating the elements in the loop, you will be able to Factor out somthing like MakeControllerElement and MakeReaderElement methods. This will help the readability / maintainability of the code (always a problem when creating classes to build XML in my experience).
|
|
|
|
|
Will keep that in mind... thanks again.
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
--- XElement Code ---
foreach (var controller in ControllerList)
{
xElement.Element("plant").Element("building").Add(new XComment(String.Format("Controller: {0} Declaration", controller.Key)));
XElement con = new XElement("controller",
new XAttribute("name", controller.Value.ControllerName),
new XAttribute("description", controller.Value.ControllerDescription),
new XAttribute("area", controller.Value.ControllerArea),
new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(controller.Value.StartDate)),
new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(controller.Value.EndDate)),
new XAttribute("updatemode", controller.Value.UpdateMode)
);
foreach (var reader in controller.Value.ReaderList)
{
con.Add(new XElement("reader",
new XAttribute("name", reader.Value.ReaderName),
new XAttribute("readergroup", reader.Value.ReaderGroup),
new XAttribute("host", reader.Value.ReaderHost),
new XAttribute("type", reader.Value.ReaderType),
new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(reader.Value.StartDate)),
new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(reader.Value.EndDate)),
new XAttribute("description", reader.Value.ReaderDescription)
));
}
xElement.Element("plant").Element("building").Add(con);
}
<br />
<br />
<div class="ForumSig"><br />
<table width="100%" align="center"><tbody><tr><td> <font color="#FF9900">The only programmers that are better C# programmers, are those who look like this -> :bob:</font></td></tr></tbody></table> <br />
<br />
<font color="#429DFF"><br />
:java: Programm3r</font><br />
My Blog: <a href="http://www.codeproject.com/script/profile/whos_who.asp?msg=1917620&id=2823228#xx1917620xx">^_^</a></div>
|
|
|
|
|
How about:
XElement CreateReaderElement(ReaderType reader)
{
return new XElement("reader",
new XAttribute("name", reader.ReaderName),
new XAttribute("readergroup", reader.ReaderGroup),
new XAttribute("host", reader.ReaderHost),
new XAttribute("type", reader.ReaderType),
new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(reader.StartDate)), new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(reader.Value.EndDate)),
new XAttribute("description", reader.ReaderDescription));
}
XElement CreateControllerElement(ControllerType controller)
{
new XElement("controller",
new XAttribute("name", controller.ControllerName),
new XAttribute("description", controller.ControllerDescription),
new XAttribute("area", controller.ControllerArea),
new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(controller.StartDate)),
new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(controller.EndDate)),
new XAttribute("updatemode", controller.UpdateMode));
}
void AddComment(XElement target, string format, params object[] args)
{
target.Add(new XComment(String.Format(format, args)));
}
void Foo(string[] args)
{
XElement xElement;
foreach (var controller in ControllerList)
{
AddComment(xElement.Element("plant").Element("building"), "Controller: {0} Declaration", controller.Key);
XElement con = CreateControllerElement(controller.Value);
foreach (var reader in controller.Value.ReaderList)
con.Add(CreateReaderElement(reader.Value));
xElement.Element("plant").Element("building").Add(con);
}
}
Where the italicised stuff are types / method names I can't determine.
|
|
|
|
|
Great stuff ... Thanks for all your input.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hi all,
I have written all the data to the XML file. Now I read the XML data in to a XElement. I want to add a new controller to the second plant within the XML data, but it keeps adding it to the first plant. I know why ... I just don't know how I can add it to the second plant ... could anyone please help me out with this one?
<root>
<!--First Plant-->
<plant>
<!--First Building in Plant 1-->
<building>
<controller>
<reader/>
</controller>
<controller>
<reader/>
</controller>
</building>
<!--Second Building in Plant 1-->
<building>
<controller>
<reader/>
</controller>
<controller>
<reader/>
</controller>
</building>
</plant>
<!--Second Plant-->
<plant>
<!--First Building in Plant 2-->
<building>
<controller>
<reader/>
</controller>
<controller>
<reader/>
</controller>
</building>
</plant>
</root>
Many thanks in advance
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
What the heck is an XElement?
|
|
|
|
|
|
Hai friends,
I tried to open IE using the following code,
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
IWebBrowserApp wb = (IWebBrowserApp) ie;
wb.Visible = false;
object o = null;
wb.Navigate(url, ref o, ref o, ref o, ref o);
My questions is, using similar logic can I move IE to back and Forward.
Please suggest me any method to that.
|
|
|
|
|
Hey
i can suggest, you can have two stacks for Forward and Backward
using that stack the URLs and navigate programaticaly
thanks
|
|
|
|
|
Thanks for your reply jasome,
But I want to use "GoBack()" and "GoForward()" functions of "SHDocVw.InternetExplorer".
|
|
|
|
|
I want to connect to a DataBase and then add table to this database in runtime,
my code is:
private void button1_Click(object sender, EventArgs e)
{
string strsql = "create table tbl_" + textBox1.Text + "(tbl_id bigint identity(1,1) primary key)";
SqlConnection connection = new SqlConnection(@"Data Source=YOUR-6BBD24890A\Q;Initial Catalog=sepehr;Integrated Security=True;User Instance=True");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = strsql;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
toolStripStatusLabel1.Text = "Table Added To DataBase";
}
what's this error???
A user instance was requested in the connection string but the server specified does not support this option.
Changed database context to 'master'.
Changed language setting to us_english.
please help me as soon as possible
|
|
|
|
|
|
Hi.
I've created a windows service and installed it. ( I saw it in Computer Management -> Services )
Then I've started it with following code :
static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
There are two methods in my windows service :
protected override void OnStart()
{
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\mds.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
m_streamWriter.Flush();
}
protected override void OnStop()
{
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\mds.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n");
m_streamWriter.Flush();
}
But nothing is done !!!
I mean when I start it OnStart() doesn't execute.
What's wrong with it ?
Could you please guide me ?
Thanks.
|
|
|
|
|
Is this service installed? Are you able to start it from service control manager? Are you getting any error?
|
|
|
|
|
N a v a n e e t h wrote: Is this service installed?
Yeah, It was installed currently, I can see it in Computer Management -> Services but I can't start it from there. I don't know why and I don't getting any errors !
BTW : I've set StartType property to Automatic .
modified on Thursday, September 3, 2009 7:30 AM
|
|
|
|
|
Try to wrap the file writing code in try/catch and write the exception in to event log. Also try with a hardcoded path something like C:\mcb.txt . I guess problem will be with the line Environment.GetFolderPath(Environment.SpecialFolder.Desktop) . You may need to turn Allow service to interact with Desktop option. Right click on your service and choose Properties -> Logon Tab -> Allow service to interact with desktop .
Your file writing code can also be improved. Here is an improved version.
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\mds.txt")
using(StreamWriter writer = File.CreateText(path))
{
writer.WriteLine("mcWindowsService: Service Started \n");
}
|
|
|
|
|
Great, It works.
Thanks a lot N a v a n e e t h
I've test with C drive.
N a v a n e e t h wrote: You may need to turn Allow service to interact with Desktop option. Right click on your service and choose Properties -> Logon Tab -> Allow service to interact with desktop.
Where is Logon Tab ?
I've found it. Can we do it programmatic ?
modified on Friday, September 4, 2009 6:56 AM
|
|
|
|
|
Mohammad Dayyan wrote: I've found it. Can we do it programmatic ?
Not sure.
|
|
|
|
|
|
How do you know OnStart() doesn't execute? Is it because nothing is written to the file? I think you need to close your StreamWriter too, not only flush it.
|
|
|
|
|
Calla wrote: How do you know OnStart() doesn't execute?
If it executes , the file have to exist in Desktop , but the file doesn't create.
|
|
|
|
|
You need to find out whether the service is installed correctly or if you just have trouble creating and writing to the file.
Try writing something to the Application Log in the OnStart method of the service to verify the code is being run.
protected override void OnStart()
{
System.Diagnostics.EventLog.WriteEntry("In OnStart.", EventLogEntryType.Information);
}
|
|
|
|
|
Seems as the service is not installed correctly.Use InstallUtil,ServiceInstaller and ServiceProcessInstaller to figure it out.To debug the service you should open the project first and next use Attach to Process from tools menu to attach the service.
Life is a stage and we are all actors!
|
|
|
|
|