Browsing 7239 questions and answers with Jon Skeet

Reading XML in a string and write to text file in c#

I have XML content that I am retrieving from a service and I want to write it into a text file. I am getting this XML content in a string variable. How can I read this and write...
Jon Skeet
people
quotationmark

If you want to save it in a file, don't use DownloadString to start with - just use WebClient.DownloadFile. If you really want to fetch it in memory and then save it, you can save it with: File.WriteAllText(filename, results); Note... more 9/20/2013 11:51:35 AM

people

Reading specific values from XML response generated by google maps api

I am trying to get address from the XML file returned by google map api (by giving lat/lng as parameters). I am using the following code. XDocument doc =...
Jon Skeet
people
quotationmark

You haven't shown us the XML involved, but I strongly suspect the problem is that you're not specifying the namespace. You probably want something like: XNamespace ns = "some namespace URI"; XDocument doc = XDocument.Load("uri"); var... more 9/20/2013 11:36:11 AM

people

Why the C# property initializer (new()) is written as that?

Info myPath = new Info() { path = oFile.FileName }; ... class Info { public string path; public string Path { ...
Jon Skeet
people
quotationmark

That construct is an object initializer. It's not a list of arbitrary statements - it's only initialization of fields and properties, and they're comma-separated: Foo x = new Foo // Implicitly calls the parameterless constructor { ... more 9/20/2013 11:21:11 AM

people

How do i Swap Data of an Arrays?

public class Swap_Numbers { public static void main(String[] args) { int numTens[] = {1, 2, 3, 4, 5}; // First array of numbers int...
Jon Skeet
people
quotationmark

I just don't know what codes should i use to swap the data of numTens and numHundred without using extra variables You shouldn't, basically. Just take the simple route of a temporary variable: int[] tmp = numTens; numTens =... more 9/20/2013 8:49:42 AM

people

How to figure out dynamically all methods with custom attribute

I have a simple challenge. I dynamically need to figure out all methods with a specific attribute in C#. I'm going to load the assemblies dynamically from another application and...
Jon Skeet
people
quotationmark

The simplest approach is to use MethodInfo.IsDefined - quite possibly with LINQ as well: var testMethods = from assembly in assemblies from type in assembly.GetTypes() from method in type.GetMethods() ... more 9/19/2013 9:32:16 PM

people

Will accesing static object/resources through static methods have performance issue?

I have a class which reads an xml file and populates them in a private static data-structure(say, HashMap). This initial population happens in a static block. Then I have method...
Jon Skeet
people
quotationmark

Firstly, it's worth being aware that this really has very little to do with static. There's no such thing as a "static object" - there are just objects, and there are fields and methods which may or may not be static. For example, there... more 9/19/2013 12:09:31 PM

people

Right way to reinitialise transient variable

Assume a simple serializable Object like this: public class MySerializable implements Serializable{ String value; transient String test = "default"; public...
Jon Skeet
people
quotationmark

From the docs for Serializable: Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: private void writeObject(java.io.ObjectOutputStream... more 9/19/2013 11:13:17 AM

people

Parsing the DateTime format to get Format String

I would like to be able to get the format string from a DateTime string. e.g. "2012-12-08 15:00:00" = "yyyy-MM-dd HH:mm:ss" "2013/30/01 16:00" = "yyyy/dd/MM HH:mm" Is...
Jon Skeet
people
quotationmark

It would be very hard to do this in a completely general way, but one option would be to extract the relevant DateTimeFormatInfo that you're interested in (using CultureInfo.DateTimeFormat), extract the culture-specific patterns from that... more 9/19/2013 10:13:54 AM

people

casting to an unknown type reflection in C#

the problem I am currently having is that I am trying to cast to an unknown type and I receive this message from the following code: The type or namespace name 'thistype'...
Jon Skeet
people
quotationmark

You don't need to cast at all, assuming the value of properties[i] is already actually the right type: for (int i = 0; i < items.Length; i++) { typeof(BugManagerQueryOptions).GetProperty(items[i].ToString()) ... more 9/19/2013 8:40:50 AM

people

can I access static field in static block before declaration?

I am getting confused with below code I expected that it will give an error or answer will be 10 but it is giving 20 how? public class test { public static void...
Jon Skeet
people
quotationmark

It's specified in section 12.4.2 of the JLS, which gives details of class initialization: Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual... more 9/19/2013 8:06:22 AM

people