Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
XML to C# object returning error:
Error is : Data at the root level is invalid. Line 1, position 1.
How to deserialize xml string to c# object?
Here is my XML:
<MSGIDRETURN>
<VERSION>1.0</VERSION>
<MSGID_LIST>
<MSGID>Test1234567</MSGID>
</MSGID_LIST>
</MSGIDRETURN>
Here is my C# Classes:
[XmlRoot("MSGIDRETURN")]
public class MSGIDRETURN
{
[XmlElement("VERSION")]
public string Version { get; set; }
[XmlElement("MSGID_LIST")]
public MSGID_LIST MsgIdList { get; set; }
}
[Serializable()]
public class MSGID_LIST
{
[XmlElement("MSGID")]
public List<string> MsgId { get; set; }
}
And Deserialization Code :
XmlSerializer serializer = new XmlSerializer(typeof(MSGIDRETURN));
StringReader rdr = new StringReader(inputString.Trim());
MSGIDRETURN resultingMessage = (MSGIDRETURN)serializer.Deserialize(rdr);
Just tried your solution, with string instead of input and it's working.
What is your inputString? Is that file or something else?
string testData = #"<MSGIDRETURN>
<VERSION>1.0</VERSION>
<MSGID_LIST>
<MSGID>Test1234567</MSGID>
</MSGID_LIST>
</MSGIDRETURN>";
XmlSerializer serializer = new XmlSerializer(typeof(MSGIDRETURN));
StringReader rdr = new StringReader(testData.Trim());
MSGIDRETURN resultingMessage = (MSGIDRETURN)serializer.Deserialize(rdr);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm fairly new to c# and I'm trying to create a custom object, but they always seem to be empty.
public class Item
{
public string itemName {get;set;}
public int itemAmount {get;set;}
public Item (string name, int amount)
{
name = itemName;
amount = itemAmount;
}
}
public class Backpack : MonoBehaviour
{
void Start()
{
Item gold = new Item ("gold", 5)
}
}
When I try to get gold parameters I get null, 0. Should it work like that? I wanted to use it to quickly add items to a list, and right now I would have to change all of them manually with something like
gold.itemName = "gold"; gold.itemAmount = 5.
Can I do it in another way?
Your assignment in the constructor is the wrong way around, instead it should be:
public Item (string name, int amount)
{
itemName = name;
itemAmount = amount;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a dynamic object that I have to convert to json (anonymous) for passing to an API as param. What is the best way to do this conversion?
There is this post:
How to convert a dynamic object to JSON string c#?
But this does not quite apply to me as I cannot use var as explained in the comment below.
Thanks
Anand
Trying to covert
dynamic d = new ExpandoObject()
d.prop = "value"
To:
var json = new {prop = "value"}
If you are in .NET Core You can use System.Text.Json;and serialize as dynamic
public static void Main()
{
dynamic w = new ExpandoObject() { Date = DateTime.Now, Item1 = 30 };
w.Item2 = 123;
Console.WriteLine(JsonSerializer.Serialize<dynamic>(w));
}
class ExpandoObject
{
public DateTimeOffset Date { get; set; }
public int Item1 { get; set; }
public int Item2 { get; set; }
}
fiddle here
But I do not understand why do you need to use dynamic (probably there is some logic behind it)
if you are in .Net Framework you need to use Newtonsoft Json which is basically the same stuff
dynamic results = JsonConvert.SerializeObject<dynamic>(w);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I knew constructors from Java and have now a C# project. Syntax in both languages is very similar, so I thought this shouldn't be a problem:
class ShapeItems
{
public String masterName = "";
public String stencilName = "";
public Double coordY = 0.0;
public Double coordX = 0.0;
public String shapeText = "";
public void ShapeItems(String mN, String sN, Double X, Double Y, String sT)
{
this.masterName = mN;
this.stencilName = sN;
this.coordX = X;
this.coordY = Y;
this.shapeText = sT;
}
}
But as I wrote the constructor, I received the error:
Member Names cannot be the same as their enclosing type
I've seen some others with this problems here, but the answers won't fix my problem.
Maybe someone here has a hint for me to solve this issue?
You don't have a constructor but a method: void. Remove the word void and it should work.
So
public ShapeItems(params) { }
instead of
public void ShapeItems(params) { }
Remove "void" from constructor signature:
public ShapeItems(...) { }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Of course it's easy to write the code to deserialize from this format. I've already done it, but I don't like.
The single responsibility principle states that I should have a generic class that worries only about this kind of serialization. And the task is generic enough to be coped by a framework.
If you converted it to a JSON string like (which should be easy)
var jsonArray = “[{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}]”;
then you could easily deserialize it with Json.NET into whatever you want and Json.NET takes care of converting the values to the right types for you:
MyType1[] result = JsonConvert.Deserialize<MyType1[]>(jsonArray);
MyType2[] result = JsonConvert.Deserialize<MyType2[]>(jsonArray);
public class MyType1
{
public string key { get; set; }
public string value { get; set; }
}
public class MyType2
{
public string key { get; set; }
public double value { get; set; }
}
or even just as a dictionary (I hope I have the syntax correct, I didn't test it):
var jsonDic = “{{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}}”;
var result = JsonConvert.Deserialize<Dictionary<string, string>>(jsonDic);
The single responsibility class (just as an example):
public class KeyValueParser
{
public static TResult ParseKeyValueString<TResult>(string keyValueString)
{
keyValueString = ConvertToJson(keyValueString);
TResul result = JsonConvert.DeserializeObject<TResult>(keyValueString);
return result;
}
private static string ConvertToJson(string keyValueString)
{
// convert keyValueString to json
}
}
usage
var jsonDic = “{{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}}”;
var result = KeyValueParser.ParseKeyValueString<Dictionary<string, string>>(jsonDic);
I don't really understand the question.
If it is something your program does a lot then move the function to some area that it is easy to get too (or a nuget package if a lot of your systems need it). If it happens in one place in your code put it quite close to that place.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like to create a new object that is the instance of the following class.
How to make the object created by relfection equals the instance of the object represented by the class below with reflection c #?
public class cPerson
{
public String name { set; get; }
public String adress { set; get; }
public String phone { set; get; }
}
Maybe you're looking for something like Activator:
var person = Activator.CreateInstance(typeof(cPerson));
Of course, you'll probably be using it when the type is unknown at design time, to create object of the same type...
var newInstance = Activator.CreateInstance(p.GetType());