|
I want to create an xml and put it to a string. I noticed that XmlWriter always adds an xml header and an encoding="..." attribute to this header. This encoding attribute becomes erroneous if I save the string as a file with some other encoding. Is there a better way to skip this encoding attribute than the one below?
StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder);
writer.WriteStartDocument();
writer.Flush();
builder.Replace(" encoding=\"utf-16\"", "");
writer.WriteStartElement("foo");
writer.WriteStartElement("bar");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
return builder.ToString();
The result of this is:
<?xml version="1.0"?><foo><bar /></foo>
|
|
|
|
|
I found another, similar way, using XmlWriterSettings.OmitXmlDeclaration:
StringBuilder builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\"?>");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (XmlWriter writer = XmlWriter.Create(builder, settings))
{
writer.WriteStartElement("foo");
writer.WriteStartElement("bar");
writer.WriteEndElement();
writer.WriteEndElement();
}
return builder.ToString();
|
|
|
|
|
hai all,
in c# is there any way to count the number of occurence of a specific character in a string?
my string is a,b,c,d,e,f
i need to count the number of commas in this string
All I ever wanted is what others have....
CrazySanker
|
|
|
|
|
hi,
u can use this code:
<br />
String str = "a,b,c,d,e,f";<br />
String []count = str.Split(',');<br />
int numOfOccurance = count.Length - 1;<br />
hope it helps
Jamil abou khalil
|
|
|
|
|
string x = "a,b,c,d,e,f";
int y = x.Split(',').Length - 1;
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Or:
Regex regex = new Regex( "," );
int count = regex.Matches( input ).Count;
----------------------------
Be excellent to each other
|
|
|
|
|
Easiest:
int cnt = str.Length - str.Replace(",", "").Length;
Fastest and most resource efficient:
int cnt = 0;<br />
foreach (char c in str) {<br />
if (c == ',') cnt++;<br />
}
---
b { font-weight: normal; }
|
|
|
|
|
Hi,
I need to get some knowledge about the printing procedure in C# using VS 2005. I want to know how to use the print dialog and print document and any other necessarly object to print the text inside a text area (or multi-lined text box).
Thanks in advance;
jamil abou khalil
|
|
|
|
|
Hello
Did you revise the printing section in C# articles page?
Regards
|
|
|
|
|
hi,
I tried searching in the articles but i am not lucky to find anything.
If you can please point me to an article that may benefit me.
thanks
jamil abou khalil
|
|
|
|
|
I'm developing an Outlook Add-in with Visual Studio 2005 using COM.
I created a new project via Extensibility -> shared add-in, and I need to add the reference "Microsoft.Office.Interop.Outlook" in order to go on with the development. But it is no where to be found in the "Add Reference" Dialog Box.
Somebody help me, please......
|
|
|
|
|
I solved it myself...
The assembly is right there in:
COM -> Microsoft Outlook 11.0 Object Library......
|
|
|
|
|
I am creating a custom draw treeview with level 0 treenodes draw by the code below. I want to paint the entire node row, including behind the PlusMinus button with a color of choice.
The problem happens with my code running after the PlusMinus button is painted,thus erasing it. Is there some event fired before OnDrawNode painting the "+" that i am missing and should override?
Setting a breakpoint on the begining of OnDrawNode and seeing the control, shows me the "+" is already painted.
protected override void OnDrawNode(DrawTreeNodeEventArgs e){
if (e.Node.Level == 0){
Rectangle nodeRegion = new Rectangle(ClientRectangle.X, e.Node.Bounds.Y, ClientRectangle.Width, e.Node.Bounds.Height);
SolidBrush nodeBackColor = new SolidBrush(Color.BlanchedAlmond);
SolidBrush nodeForeColor = new SolidBrush(Color.Black);
Font nodeFont = (e.Node.NodeFont == null) ? this.Font : e.Node.NodeFont;
e.Graphics.FillRectangle(nodeBackColor, nodeRegion); // PROBLEM
e.Graphics.DrawString(e.Node.Text.Trim(), nodeFont, nodeForeColor, e.Node.Bounds.Location);
}
else{
e.DrawDefault = true;
}
base.OnDrawNode(e);
}
ps.: It looks like the OnPaint event isnt fired on a TreeView. Couldnt find why is this on MSDN.
Thanks!
Diego Valdevino
|
|
|
|
|
I have a UserControl and when Iam not using this.Region to form a custom shape for my UserControl everything is ok except that in the upper left and right corner you can see a cotrol background.
See here: http://img87.imageshack.us/my.php?image=goodjo2.jpg
When I put this piece of code:
region.Intersect(gp);
this.Region = region;
Where the gp looks like:
gp.AddArc(0, 0, 20, 20, 180, 90);
gp.AddArc(this.Width - 21, 0, 20, 20, 270, 90);
gp.AddLine(this.Width - 1, 20, this.Width - 1, this.Height - 1);
gp.AddLine(this.Width - 1, this.Height - 1, 0, this.Height - 1);
gp.CloseFigure();
In other words, those darker blue lines (borders) of the UC, I get this ugly looking corners in upper left and right corners and no right and bottom line.
See here: http://img291.imageshack.us/my.php?image=badid4.jpg
My question is, how can I set an Region to my UC without this 'stuff' around upper left and right corners and with right and bottom lines?
I hope you understand.
|
|
|
|
|
|
Hello,
I'm using Application.Exit(); in the form contructor to terminate the application if certain condition is met... but it's not working!
Why? and what can I do?
Thanks...
|
|
|
|
|
Hello
I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right?
Well if you take a look ate Program.cs file where the main method exists, you'd see this line
Application.Run(new MyForm());
So actually it excutes more like this:
MyForm temp = new MyForm()
Application.Run(temp)
Well, not exactly but I made it this way to illustrate that your constructor must return first before the Run() method actually gets called, and of course calling the Exit() method is useless if the Run() didn't get excuted first.
Suggested solution:
Simple; Make the Application.Exit() call in your Form_Load event if applicable.
Regards
|
|
|
|
|
|
Bad answer -- you can't Application.Exit() in Form_Load either. Only solution is change of architecture: don't start the form if you don't want to display it
|
|
|
|
|
Hello .
I am looking for a program or
an idea how to parse C# files .
What I mainly need to find is disposing of created objects in the methods
In each method in each class that is in the file ,
I want to look for object creation statement :
myObject = new CMyObject()
And then to see whether this object is disposed in the end of the method
( in the finally ) .
And I also want to analyze more stuff .
The question is whether anyone knows a program that can help me ,
or can give me an idea how to do it ?
CodeDom can help me ?
Is it possible to create CodeDom objects hierarhy from source code ?
Thanks
|
|
|
|
|
Hello
Well, regular expressions is pretty much what you are looking for. Use it to identify the pattern of creation, then extract the object's name from it. Using that name you can search for disposal code if exists. Since I'm not that good in Regex, I wouldn't be much helpful in phrasing your Regex, but if you don't know Regex:
The namespace you'd use : System.Text.RegularExpressions
A very good tutorial site: This site[^]
VoidPointer wrote: CodeDom can help me ?
CodeDom would be a choice if you want to modify the code and compile it through your program. But if you just want to search and modify something in the cs files -which could be much treated as text files-, then you wouldn't need CodeDom.
Ps. You application sounds pretty much as a Visual Studio Add-in!! Is that what you're trying to make?
Regards
|
|
|
|
|
Thanks .
Let's see whether others have some ideas too .
I mainly need this for my personal use .
( I have a huge project and we add modules all the time to it
and checking this manually is not much fun )
Though maybe I'll convert this functionality ( when and if I'll write it )
into an add-in for VS .
|
|
|
|
|
i have two Forms : Form1 and Form2
from Form1 i show Form2:
Form2 form2 = new Form2();
form2.Show();
i want from Form2 to Close Form1 by using the same code:
Form1 form1 = new Form1();
form2.Show();
but doesn't close Form1
|
|
|
|
|
Hello
I don't know why someone flamed you with score 1.0, and didn't even answer you -wasn't me btw-. Your question is quite very simple -forgive me for saying that-. I guess you didn't do much reading in OOP did you?;)
The first block of code is fine in creating a form, while the second is far from closing one:
m.m._2007 wrote: Form1 form1 = new Form1();
The above line creates a new form of type Form1. So refereing to it would be to the new form not your original form.
m.m._2007 wrote: form2.Show();
And this line shows the new form that you've just made in the previous block. Why on earth did you expect it to close the original one? Shouldn't you've called the method Close() at least?
To make your code work you have to pass reference to your original form to the Form2 object and call the Close() method for it.
Edit your Form2 constructor to
private Form1 orgigenal
Form2(Form1 or)
{
origenal = or;
}
Now show Form2 like this:
Form2 myForm = new Form2(this);
and in Form2 close Form1 like this
origenal.Close();
I hope you got it, and I strongly suggest you do more reading in C# OOP programming.
-- modified at 21:59 Tuesday 24th October, 2006
Regards
|
|
|
|
|
form2 is builded by form1
You close form1 in form2,and the application will terminate
The code like this:
Code in Form1:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.Frm = this;
form2.Show();
}
Code in Form2:
///
/// Form which will close
///
private Form _frm = null;
public Form Frm
{
get
{
return _frm;
}
set
{
_frm = value;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Frm.Close();
}
phenix-burn
|
|
|
|