|
It seems that you haven't added the parameter to the report itself
|
|
|
|
|
Hello all,
I have a curious problem relating to XML deserialisation where the deserialiser allows malformed XML to pass through without issue. This needs some further information, so without further ado:
I have the following simple schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestSchema">
<xs:complexType>
<xs:sequence>
<xs:element name="Widgets" type="xs:string" nillable="false"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
From this I have created .NET 2 c# classes using the XSD tool, this result in the following class
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class TestSchema {
private string widgetsField;
public string Widgets {
get {
return this.widgetsField;
}
set {
this.widgetsField = value;
}
}
}
This is then added to a simple test project that looks like this:
static void Main(string[] args)
{
string rawXml = "<TestSchema>";
rawXml += "<Widgets>A value for widgets</Widgets>";
rawXml += "<LaLa>An element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
byte[] data = System.Text.Encoding.ASCII.GetBytes(rawXml);
MemoryStream fs = new MemoryStream(data);
TextReader reader = new StreamReader(fs);
XmlSerializer ser = new XmlSerializer(typeof(TestSchema));
TestSchema obj = (TestSchema)ser.Deserialize(reader);
reader.Close();
Console.WriteLine(obj.GetHashCode());
Console.ReadLine();
}
Now all this works, however I wouldn't expect it to! The more eagled eyed will have noticed the following line in the code above:
rawXml += "<LaLa>An element that doesnt exist in the schema</LaLa>";
When the deserialiser is processing the XML string, it seems that any elements that are not in the C# schema class are simply ignored. So the first question is, does anybody know of a way to run the deserialiser in "strict" mode, so that if an unknown element is present then an exception occurs.
When testing different scenarios I also discovered that I could remove a nillable false element and still deserialise, which resulted in the object value being, obviously, null. For example :
string rawXml = "<TestSchema>";
rawXml += "</TestSchema>";
or
string rawXml = "<TestSchema>";
rawXml += "<LaLa>Hold on whats this an element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
So the second questions is, is there any way to run the deserialiser in "strict" mode where if a mandatory/nillable false element is not found and exception occurs.
An immediately obvious solution to this problem is to pass the XML through a validator first and then perform the deserialisation. While this will work, in the real world the application is processing alot of XML data and having to validate every one will add to processing time. I would prefer to only validate if a deserialisation exception occurs.
-- modified at 10:31 Thursday 8th November, 2007
|
|
|
|
|
It would seem I have found the solution, a tweak to the code above and malformed XML is no longer allowed:
static void Main(string[] args)
{
string rawXml = "<TestSchema>";
rawXml += "<Widgets>A value for widgets</Widgets>";
rawXml += "<LaLa>Hold on whats this an element that doesnt exist in the schema</LaLa>";
rawXml += "</TestSchema>";
XmlSerializer s = new XmlSerializer(typeof(TestSchema));
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(rawXml);
MemoryStream ms = new MemoryStream(buffer);
XmlReader reader = new XmlTextReader(ms);
XmlDeserializationEvents deserializationEvents = new XmlDeserializationEvents();
deserializationEvents.OnUnknownAttribute = new XmlAttributeEventHandler(UnknownAttributeEventHandler);
deserializationEvents.OnUnknownElement = new XmlElementEventHandler(UnknownElementEventHandler);
deserializationEvents.OnUnknownNode = new XmlNodeEventHandler(UnknownNodeEventHandler);
deserializationEvents.OnUnreferencedObject = new UnreferencedObjectEventHandler(UnreferencedObjEventHandler);
TestSchema obj = (TestSchema)s.Deserialize(reader, deserializationEvents);
reader.Close();
Console.WriteLine(obj.GetHashCode());
Console.ReadLine();
}
private static void UnknownAttributeEventHandler(object sender, XmlAttributeEventArgs e)
{
}
private static void UnknownElementEventHandler(object sender, XmlElementEventArgs e)
{
}
private static void UnknownNodeEventHandler(object sender, XmlNodeEventArgs e)
{
}
private static void UnreferencedObjEventHandler(object sender, UnreferencedObjectEventArgs e)
{
}
As you can see in the code above, the XmlSerializer.Deserialize can also take an instance of XmlDeserializationEvents. If this is setup and passed in these events are created and from there I can handle the error condition.
|
|
|
|
|
Good to know... thanks for posting that!
|
|
|
|
|
Since posting the original solution I have found that you can also set these event directly on the XmlSerializer
XmlSerializer xmlSerializer = new XmlSerializer(objectType);
xmlSerializer.UnknownAttribute += new XmlAttributeEventHandler(xmlSerializer_UnknownAttribute);
xmlSerializer.UnknownElement += new XmlElementEventHandler(xmlSerializer_UnknownElement);
xmlSerializer.UnknownNode += new XmlNodeEventHandler(xmlSerializer_UnknownNode);
xmlSerializer.UnreferencedObject += new UnreferencedObjectEventHandler(xmlSerializer_UnreferencedObject);
From a functionality point of view I don't suppose it makes any difference at all, however IMHO this method is neater.
|
|
|
|
|
hello,
un have 2 application and i send messages between them using named pipes technologie;
i send 2 messages , the first for showing the application and the second for hiding it;
the probleme is before sending the messages i have to know the state of application (show/hide).
how can i know the state of my second application?
|
|
|
|
|
You'll need a method that queries the state of the second application.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
whitch method do you talking about? i can figure out
please help
|
|
|
|
|
A method which you have to write in order to figure out the state of the application !
AliAmjad(MCP)
First make it Run THEN make it Run Fast.
|
|
|
|
|
i m so sorry, but i dont understand what do you mean,
i want to know can i know the state of a dialog or application (hide/show) ?
if yes , how?
thanks
|
|
|
|
|
This is easy. Create a method that checks if the form is visible or not.
(Hint: use the Visible property)
Kristian Sixhoej
"Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
Check to see if the form is visible.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
i want to know if is visible from another application not the same application
i hope that now that s is more clear
|
|
|
|
|
Morad SAJID wrote: i want to know if is visible from another application not the same application
i hope that now that s is more clear
How can I put this simply? If you want to know if application B is visible or not, then application A is going to have to query it and ask it if it is visible or not. Hence, application B will have to provide this information. That's what we've been telling you all along. There's no easy way for the first application to know this - it's the responsibility of the second app to tell it.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
yes thank you
i have an idéa but i don't know if it will be works;
i ll change the process name of the application A, the application B will looking for the first name of application A , when it will not find it it will know that my application is hided,
but how to change the processname of my application;
thakks again
|
|
|
|
|
I would have the application that shows/hides create a named Mutex when it is visible. It can release the Mutex when it gets hidden.
The calling app can try and create the same named Mutex and if it fails, it knows the application is visible. If it succeeds, it releases the Mutex and knows that the application is hidden.
|
|
|
|
|
Hi Guys. I need a bit of help. I want to put a sql server connection together but don;t know how. I have only previously worked with .MDF files but now want to put a connection together to a SQL server. The details of the server is as such.
Server - JBSQL1\DEV1
DataBase - CSIS
How do I use this to create a sql server connection? Or do you guys know of any documentation that states clearly how to do this as I have looked, googled and nothing seems clear and straight forward.
Any help would be greatly appreciated.
|
|
|
|
|
Kwagga wrote: I want to put a sql server connection together but don;t know how.
Here[^]
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
i got some error while connecting the server database(sqlserver in DATABASE sever) ....System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
when i put the code in server
but i can connect it in client side(using .net 2005 development platform)
i just use the code
SqlConnection con = new SqlConnection("Data Source=server ip;initial catalog=database name;uid=userid;pwd=password");
sqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from StLogin";
cmd.Connection = con;
SqlDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
while (dr.Read() != false)
{
DropDownList1.Items.Add(dr[1].ToString());
}
|
|
|
|
|
I'm sick of answering this one for people. Here[^], help yourself.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
imatetvm wrote: Urjent
The subject should contain a useful description of the question.
However urgent you may feel that the question is, it's never urgent enough to mention in the subject, or you would not post the question in a forum instead of paying someone for support.
imatetvm wrote: i got some error while connecting the server database
Have you considered the possible explanation that was given to you in the error message already?
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hello
within my application i have a richtextbox, when i trying to print someting it doesn't work.
Bullets are not shown on the printpreview hereby the code
// OnBeginPrint
private void OnBeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
char[] param = { '\n' };
if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = richTextBox1.SelectedText.Split(param);
}
else
{
lines = richTextBox1.Text.Split(param);
}
int i = 0;
char[] trimParam = { '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
// OnPrintPage
private void OnPrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Brush brush = new SolidBrush(richTextBox1.ForeColor);
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++],
richTextBox1.Font, brush, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
else
e.HasMorePages = false;
}
}
I hope someone canhelp me
thanks anyway
-- modified at 8:41 Thursday 8th November, 2007
Greetzzz
Rudy
|
|
|
|
|
Hi,
first of all I don't have any experience printing RTB content. But looking at your code
I can see you only take its (Selected)Text, i.e. the string representing its content,
ignoring all formatting (font selection, colors, bullets, ...). This does not look right.
May I suggest you search CP (or Google) for "RichTextBox print".
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I did not find any solution thats work properly
Greetzzz
Rudy
|
|
|
|
|
That is not very informative, you don't tell us what you did and tried, and what parts
did or did not work.
Did you search CodeProject articles? Here is one for starters[^].
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|