|
Thanks. Here's what I have from my test program. You code is at the bottom. The Xml file is empty after tnis runs:
static void Main(string[] args)
{
Product p1 = new Product { ItemId = 100, ProductName = "Hammer" };
Product p2 = new Product { ItemId = 101, ProductName = "Level" };
Product p3 = new Product { ItemId = 102, ProductName = "Screwdriver" };
InvoiceDetail details = new InvoiceDetail { ItemId = 10, InvoiceId = 4 };
details.Items.Add(p1);
details.Items.Add(p2);
details.Items.Add(p3);
InvoiceHeader ih = new InvoiceHeader { InvoiceDate = DateTime.Now };
ih.Items.Add(details);
Customer customer = new Customer {ItemId = 1, Name = "Frank Jones" };
customer.Items.Add(ih);
Customers customers = new Customers();
customers.Items.Add(customer);
string FileName = @"c:\myfile.xml";
XmlSerializer serializer = null;
try
{
serializer = new XmlSerializer(typeof(Test2.Customers));
}
catch (Exception ex)
{
}
FileStream dataFile = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
try
{
serializer.Serialize(dataFile, customers);
}
catch (Exception ex)
{
}
dataFile.Close();
dataFile.Dispose();
}
Everything makes sense in someone's mind
|
|
|
|
|
The question I have then, is what does Customer's look like? And what does your base class look like?
I would think something like:
[Serializable]
[XMLRoot("Customers")]
public class Customers: _ItemBase
{
}
[Serializable]
public abstract class _ItemBase
{
[XMLAttribute("Type")]
public ItemType Type {get; set;}
[XMLAttribute("ItemID")]
public int ItemId {get; set;}
private _Collection<_ItemBase> _Items = new _Collection<_ItemBase>()
[XMLElement("Items")]
public _Collection<_ItemBase> Items
{
get {return _Items;}
set {_Items = value;}
}
}
Is that close?
You will have some problems like this though. That's because it looks like the way you've set it up, you could add an InvoiceHeader directly to Customers, or Customers to an InvoiceHeader if everything is just inherited from that base class. In order to deserialize, it's going to need a set structure, because beyond the root, I don't think you'll be able to name anything besides "Items". I could be wrong though.
|
|
|
|
|
Thats exactly the structure.
The goal here is to have a hieraarchy of objects that all inherit from a base. This way, I would know for sure that each item
has an ItemId and and Items collection. And, any method that can recieve type ItemBase could recieve an instance of any class
that inherits from it, so this means more generic code.
There's has be a way of doing this.
Everything makes sense in someone's mind
|
|
|
|
|
Well, like I said, since they are all generic classes, there would be no way for the serializer...more specifically the deserializer to know what type to put each item into.
The only two options are to make them less generic so that you can have specific XmlElements for each type. The other option is to write your own Serializer. It's not that hard to write XML code with the XML namespace.
You could probably pretty easily write your own. You could then use the typeof or gettype method to be able to specify specific Element names. With generic classes, it might not be that hard to do. You could use getType(object).Name as the Element and then add attributes and build down.
|
|
|
|
|
I came to the same conclusion here also. One other (messy) possibility is that instead of using a base class, use the collection
class on each individual class. This way, there's no inheritence and I can specify element names in each class.
Everything makes sense in someone's mind
|
|
|
|
|
What type is _Collection?
Because that may also need the Serializable attribute set.
|
|
|
|
|
I am new to using the InternalsAvailableTo tag in AssemblyInfo.
My problem is that my code assembles and works fine, but while coding, VS flags errors.
Assembly A inherits from 'internal' members of Assembly B. So assembly B makes "A" a friend by using the InternalsAvailableTo attribute.
The program builds and executes fine.
While coding, the editor will flag errors such as 'data cannot inherit from ISomeInterface.' (ISomeInterface is declared internal).
Am I missing something?
|
|
|
|
|
I never heard about that.
When I saw your question, I tried to find...
I think the attribute you say is: InternalsVisibleTo.
But, if the editor show some error, I think it is an editor error, not error in your code.
|
|
|
|
|
I need to change the startup type of already installed Windows services. I can start and stop them with their display name and was wondering if the same can be done for the startup type? I could look in the registry I guess and just edit that but would it be the best way to go about it?
How can I tell which service is which in the registry?
|
|
|
|
|
You can gio into the service manager and change it. Right-click the service and select "Properties" in the context menu.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
I posted in C# so I could figure it out programmatically. I know how to do it by hand.
|
|
|
|
|
ziopino70 wrote: so I could figure it out
Ummm... no you didn't...
|
|
|
|
|
Hi,
0. I don't think you can change the type, other than by altering the server code itself.
1. I never used it but WMI offers a Win32_Service class; not sure what it offers exactly.
2. The ServiceController class has a ServiceType getter, no setter.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
You can modify this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName ,
value Start :
Values that will be interesting to you:
2 - automatic, 3 - manual
Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
|
|
|
|
|
|
I wouldn't use the registry to accomplish this. You could always look to use WMI to accomplish this. Here's a sample that might help (I knocked it up in Notepad, so I apologise if the syntax isn't 100% correct):
public enum StartupType
{
Automatic,
Disabled,
Manual
}
public void SetStartupType(string serviceName, StartupType startupType)
{
string type = startupType.ToString();
using (ManagementPath mp = new ManagementPath(string.Format("Win32_Service.Name='{0}'", serviceName)))
{
if (mp != null)
{
using (ManagementObject mo = new ManagementObject(mp))
{
object[] parameters = new object[1] { type };
ManagementObject.InvokeMethod("ChangeStartMode", parameters);
}
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi,
Here is an example to change the startup mode of any service.
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class CallWMIMethod
{
public static void Main()
{
try
{
ManagementObject classInstance =
new ManagementObject("root\\CIMV2",
"Win32_Service.Name='ALG'",
null);
ManagementBaseObject inParams =
classInstance.GetMethodParameters("ChangeStartMode");
inParams["StartMode"] = "3";
ManagementBaseObject outParams =
classInstance.InvokeMethod("ChangeStartMode", inParams, null);
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
}
}
}
Regards,
Sunil G.
|
|
|
|
|
Hi,
I've set my WindowStartPosition for the form to be centered. I am also changing the size of the form in the FormLoad event as some of the group boxes are not visible.
When the form displays on the screen it's centered based on the OLD size, not the new size. Is there a better place to resize the form so that it remains centered, or a way to force the centering somehow?
Thank you,
Glenn
|
|
|
|
|
|
set the Form.Size (or Width, or Height, or both) in the Form's constructor. Or, if they are constant, why not set them in Visual Designer; I use the properties pane for such purpose.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hi,
I'm trying to capture a CTRL/I key via a KeyDown event within a RichTextBox, the code is working perfect for the CTRL/B and CTRL/U keys (Bold and Underline), however the CTRL/I appears to erase the input and never gets to the KeyDown event. What do I need to do to get this to work.
if (e.KeyCode == Keys.B && e.Control)
ToggleFormat(FontStyle.Bold);
if (e.KeyCode == Keys.I && e.Control)
ToggleFormat(FontStyle.Italic);
if (e.KeyCode == Keys.U && e.Control)
ToggleFormat(FontStyle.Underline);
Thank you,
Glenn
|
|
|
|
|
Are you hooking the other events? Like KeyPreview or KeyPress? It works perfectly for me.
The only thing I have in my KeyDown event is:
if (e.Control && e.KeyCode == Keys.I)
{
MessageBox.Show("Ctrl+I");
}
So, if it's not getting there, then you must be handling the "I" KeyDown in some other method.
[UPDATE]
Sorry, I didn't see it was a RichTextBox. Just add this into your KeyDown:
if (e.KeyCode == Keys.I && e.Control)
{
e.SuppressKeyPress = True;
e.Handled = True;
ToggleFormat(FontStyle.Italic);
}
modified on Tuesday, May 11, 2010 1:40 PM
|
|
|
|
|
Hi,
Thank you for your response. It is actually executing the ToggleFormat(FontStyle.Italic); however after setting the font to Italic, it is erasing what was highlighted. CTRL/B and CTRL/U does not erase what was highlighted with the mouse.
So a better question is how do you prevent CTRL/I from erasing. I tried playing around with the AcceptTabs without any luck.
Glenn
|
|
|
|
|
Well, as I said, you would have to use the SuppressKeyPreview value within the Keydown code. Though what I found was that sometimes, it didn't work. For instance, if I set SuppressKeyPreview to true and then did a MessageBox, it went ahead and erased the line.
This was the code that I got to work:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.I)
{
e.SuppressKeyPress = true;
e.Handled = true;
SetItalic();
}
}
private void SetItalic()
{
if (richTextBox1.SelectionFont.Italic)
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
}
else
{
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
}
}
This code toggled Italic without erasing anything.
|
|
|
|
|
That did the trick!! -- Thank you very much!!
Glenn
|
|
|
|