|
They both do the same job. The first it a straight substitution, the second a regex. The first would be marginally faster to perform.
Have you read my edit on the harder solution?
|
|
|
|
|
I just did. Anyway, the easy method is more than enough for me.
I really appreciate your help.
|
|
|
|
|
That's okay. That's my alotment of regexs used up for the night!
|
|
|
|
|
Can I deserialize XML into an object by reverse-engineering the class definition from a sample XML document, if I don't have the actual class definition from which the XML was serialized?
I'm making function calls to the Google Maps API geocoder using an HTTP request. The function call returns an XML file, which I want to extract data from.
Here are the first few lines of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>1600 Amphitheatre Parkway, Mountain View,CA</name>
....
And here are the first few lines of the class definition that I reverse-engineered from the XML:
[XmlRootAttribute(ElementName = "kml", IsNullable = false)]
public class kmlJSON
{
public kmlJSON() { }
public class Response
{
public String name;
....
Here is my C# routine to deserialize the data into an object of kml class:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
XmlSerializer xs = new XmlSerializer(typeof(kmlJSON));
kmlJSON jsonObject = new kmlJSON();
jsonObject = (kmlJSON)xs.Deserialize(resStream);
When I run it, I get the following error at jsonObject = (kmlJSON)xs.Deserialize(resStream); :
[InvalidOperationException: <kml xmlns='http://earth.google.com/kml/2.0'> was not expected.]
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderkmlJSON.Read3_kml() +91
[InvalidOperationException: There is an error in XML document (1, 40).]
Any ideas why XmlDeserializer is getting stuck on the root declaration of the XML file? Am I missing something simple, or must I have the exact class definition that the XML was serialized from?
|
|
|
|
|
Are you sure that the XML is the result of serializing an object?
---
b { font-weight: normal; }
|
|
|
|
|
No, I'm not sure, and it probably wasn't. In fact, most likely it was generated by another language, like php or javascript. I didn't realize that was an issue. I figured that since an XML file was just text in XML format, with no special notation to indicate that it was serialized, it wouldn't matter whether it was serialized or generated by another method. As long as the class definition matched the XML format, I couldn't see how the deserializer would know any better. But I'm new to all this.
In any event, I wound up parsing the XML with XmlTextReader, and it wasn't too hard since I'm only extracting one node right now. But if I wanted to extract multiple nodes, or a whole data record, deserialization would be a better solution if it were possible. Is it?
|
|
|
|
|
It's probably not possible to deserialize the XML straight off, but might be done if you make some adjustments to it first.
You can try to create a class that matches the data in the XML, and serialize it to see what the XML should look like to be deserializable.
---
b { font-weight: normal; }
|
|
|
|
|
That's a good idea, thanks. I already built a class that I think matches the XML. Should be simple to serialize an object and compare it to the XML I'm trying to deserialize.
|
|
|
|
|
Hi all,
I'm writing a Web Application with C#. In this I have a DataSet which I've setup to return the user's role via a Table Adapter. So far I have this code:
UserDataTableAdapters.aspnet_RolesTableAdapter myAdapter = new UserDataTableAdapters.aspnet_RolesTableAdapter();<br />
<br />
txtRoleName.Text = myAdapter.GetRoleFromUserName("Martin").Rows[0].ItemArray[0].ToString();
Which works... but given DataSets are supposed to make data access easier, it seems a little bit clunky to me (as GetRoleFromUserName returns a DataTable, which I'm having to cast to a string after getting at the value I really want via the Rows and Items arrays. All I really want here is return the String RoleName - is there a better way of using TableAdapters and DataSets in code to return a single value?
Thanks,
Martin.
-- modified at 10:56 Sunday 25th June, 2006
|
|
|
|
|
Utini wrote: is there a better way of using TableAdapters and DataSets in code to return a single value?
No, but there is if you don't use them .
The IDbCommand interface (and thus all implementing classes) has a ExecuteScalar function which you can use to fetch a single value.
|
|
|
|
|
There's no real good place to post this, but I'm having trouble with a SQL query:
SELECT Task.Tasknumber,ScheduleItem.HoursScheduled / HourTotals.HoursScheduled AS PlannedValue, (ScheduleItem.HoursScheduled / HourTotals.HoursScheduled * 1) AS EarnedValue<br />
FROM ScheduleItem INNER JOIN (ScheduleItem.TaskId = Task.TaskId) CROSS JOIN HourTotals<br />
GROUP BY Task.TaskId,Task.Tasknumber,ScheduleItem.HoursScheduled,HourTotals.HoursScheduled
I want the value of EarnedValue to be zero of HoursNeeded in ScheduleItem is higher than zero. Anyone know a trick in SQL to do this? Or should I make that function in C#?
WM.
What about weapons of mass-construction?
|
|
|
|
|
|
Thanks that worked out great. I found out how to do cummulative queries to.
Lets see if I can get that to work too.
WM.
What about weapons of mass-construction?
|
|
|
|
|
I setup an HScrollBar as part of a panel. Let's call this ScrollBar MyScrollBar. Now to the problem: When a user scrolls (dragging the slider) the maximum value that can be reached is not the MyScrollBar.Maximum value but the value (MyScrollBar.Maximum - 9). This occured for any MyScrollBar.Maximum value I specified. Anyone having this bug, too? Anyone know, how to fix this?
Greetings
|
|
|
|
|
This is quoted from MSDN:
The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached is equal to the Maximum property value minus the LargeChange property value plus 1. The maximum value can only be reached programmatically.
So in your case LargeChange=10. You could get round this by adding LargeChange to Maximum;
-- modified at 16:12 Sunday 25th June, 2006
|
|
|
|
|
Thank you, didn't guess this is intended behaviour.
|
|
|
|
|
I have an object which is just a thin wrapper over an other object and might be created in big quantities.
something like that:
// ===== pseudo-code cample =======
class TheObject
{
int[] data;
TheWrapper Data { get { return new TheWrapper(data); } }
}
class or struct TheWrapper
{
int[] data;
public TheWrapper(int[] data)
{
this.data = data;
}
public int Length { get { return data.Length; } }
public int this[int index] { get { return data[index]; } }
public int UtiliyOneLikeSearch() {}
//... etc ...
}
// ========= end of pseudo-code sample ==========
Now it is very likely that aTheObject.Data would be called many times used for a few lines and forgot.
I thought it might be much more efficient memory wise to make TheWrapper a struct.
but somehow I feel it's quite unconventional.
what do you think?
|
|
|
|
|
Super Lloyd wrote: I have an object which is just a thin wrapper over an other object and might be created in big quantities.
Super Lloyd wrote: I thought it might be much more efficient memory wise to make TheWrapper a struct.
That makes good sense. This CP artcile[^] has compared class with struct.
Best,
Jun
|
|
|
|
|
let's do it!
|
|
|
|
|
Yes, using a struct would give you slightly less overhead, as there is one object less to create. Creating objects are not very expensive, though.
Generally a struct would only contain value types, not objects. When assigning a struct you copy all the data in it, but if it contains objects only the references will be copied. This is not consistent with how you expect a value type to work, so it might be pretty confusing.
If you want to make your code more efficient, I believe that there are other things that you could do that would increase the performance much more.
---
b { font-weight: normal; }
|
|
|
|
|
it's just 4 letters to replace, I could do it anytime
but yes, my worry is the slightly unintuitive behavior of this struct you so clearly outlined...
|
|
|
|
|
How to display all the image files in a directory? Even with the for loop below, I only get one image file displayed. I don't know if the problem lies in the location of the picturebox1. Can anyone please help?
int y = 5;
foreach (String file in files)
{
if (file.Contains(".jpg"))
{
System.Windows.Forms.PictureBox pictureBox1 =
new System.Windows.Forms.PictureBox();
picture.Controls.Add(pictureBox1);
pictureBox1.ImageLocation = file;
pictureBox1.Location = new System.Drawing.Point(6, y);
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
y = y + pictureBox1.Size.Height + 5;
}
}
|
|
|
|
|
Hi,
I can't see anything wrong with the code above. What control is picture ? I'm assuming it's a panel or something. Might be handy to post the code used to gain the list of files.
|
|
|
|
|
It's a tabpage control. I debug it through and found that the size of the picture box has not changed to the size of the picture (I guess this is what it should be by using autosize mode). From this I would assume the new picture box is added on top of the existing one with exactly the same size each time.....
|
|
|
|
|
Do you mean you get just one PictureBox with one image, or many PictureBoxes all with the same image?
|
|
|
|