Serialize an element value with its attributes - c#

I have an XML coming in this form:
<run>
<foo status="1">111.9</foo>
<fred status="0">5.5</fred>
</run>
I would like to deserialize this in either of these forms below (I'm undecided, and hoping an answer will help me decide, although I tend to prefer #1, for design aesthetics as much as anything else):
Case # 1
[Serializable]
public class DataValue
{
[XmlAttribute("status")]
public int Status { get; set; }
// I need something here, but what?
public float Value { get; set; }
}
[Serializable]
[XmlRoot("run")]
public class DataBag
{
[XmlElement("foo")]
public DataValue Foo{ get; set; }
[XmlElement("fred")]
public DataValue Fred{ get; set; }
}
When I try this, I get a value of 0 for either member foo or fred.
Case # 2
[Serializable]
[XmlRoot("run")]
public class DataBag2
{
[XmlElement("foo")]
public float Foo{ get; set; }
[XmlElement("foo")]
[XmlAttribute("status")]
public int Foo_status { get; set; }
[XmlElement("fred")]
public float Fred{ get; set; }
[XmlElement("fred")]
[XmlAttribute("status")]
public int Fred_status { get; set; }
}
It compiles but I get an InvalidOperationException while reflecting Foo_status, for which the innermost exception is "For non-array types, you may use the following attributes: XmlAttribute, XmlText, XmlElement, or XmlAnyElement."
What can I do to end up with an actual value in case #1, or no exception (and a valid value and status) for case #2?
The code for the serialization goes like this:
// Case 1
using (var sr = new StreamReader("data.xml"))
{
var xs = new XmlSerializer(typeof(DataBag));
var run = (DataBag)xs.Deserialize(sr);
Console.WriteLine("Got a run: {0}-{1}", run.Fred.Value, run.Fred.Status);
// Issue here is that value is always 0, but status is accurate
}
// case 2
using (var sr = new StreamReader("data.xml"))
{
var xs = new XmlSerializer(typeof(DataBag2));// Exception here
var run = (DataBag2)xs.Deserialize(sr);
Console.WriteLine("Got a run: {0}-{1}", run.Foo, run.Foo_status);
}
Thanks for your attention!

For case 1 you just need to mark it as XMLText:
[XmlText]
public float Value { get; set; }

You want to use [XmlText]:
Indicates to the XmlSerializer that the member must be treated as XML text when the class that contains it is serialized or deserialized.
Thus:
public class DataValue
{
[XmlAttribute("status")]
public int Status { get; set; }
[XmlText]
public float Value { get; set; }
}
Case #2 just won't work as you want. Adding [XmlAttribute("status")] to Foo_status means that Foo_status will be serialized as an attribute of DataBag2, not Foo. Applying [XmlElement("foo")] as well then says it's an element of DataBag2, which is of course in conflict with other attribute.
There's no way with XmlSerializer for an outer container type to specify an attribute to be applied to a nested element.

Related

newton json default string value not working?

When I seralise an object, for some string properties, I would like to output empty string other than ignore or output null.
According to newton's doc, I could do this:
public class Data
{
public int ProductId { get; set; }
[DefaultValue("")]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
public string LargeData { get; set; }
}
However, in my test case, it still output null
Data D1 = new Data()
{
ProductId = 1
};
var b = JsonConvert.SerializeObject(D1);
The output is {"ProductId":1,"LargeData":null}. Am I doing something wrong?
Looking at DefaultValueHandling it doesn't look like there's any way of doing what you want.
The default value attribute is only used when deserializing, if the property isn't specified in the JSON. The ignore / include choices are the ones which are relevant when serializing, and those don't affect the value that's serialized - just whether or not it should be serialized.
Unless you've got code which actually sets the value to null, the simplest option would be to make the property default to "" from a .NET perspective:
public class Data
{
public int ProductId { get; set; }
public string LargeData { get; set; } = "";
}

Sort on unknown object.property WPF

Let's say I have these 2 classes:
public class Foo {
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
public string SomeTextPropery { get; set; }
public object Tag { get; set; }
}
public class SomeObject {
public string Description { get; set; }
public string Description2 { get; set; }
}
And in my console application I do something like this:
var listOfFoos = new List<Foo>( );
//listOfFoos.Add(foo1) x 10
foreach (var foo in listOfFoos)
{
var someObject = new SomeObject( );
foo.Tag = someObject;
}
listOfFoos.OrderBy(x => x.Tag.Description2)
So the last part (OrderBy) won't work, because we don't know of what type Tag is.
But in my console application (frontend) I know what I set my tag to and I should be able to use a property on the Tag object in OrderBy. Is there away I can sort on Description2 when Tag is of object type?
You will need to cast the Tag property to the appropriate type:
listOfFoos.OrderBy(x => ((SomeObject)x.Tag).Description2);
Of course this requires you to actually know what the type of the Tag property is at runtime.
But if you want to order by a property called Description2, the Tag property must return an object that has this property anyway.
Maybe a common base class for all involved types, where the Description2 property is defined, is the solution to your issue? Then you can change the type of the Tag property from object to this particular type and avoid the casting.

c# JSONConvert.DeserializeObject<List<RootObject>> returns nulls

I am trying to deserialize an HTTPWebRequest response (Json) to a c# object/class, but am having trouble. A collection with 10 instances of the object are returned, and all the objects are null.
Here is the json:
[
{
"id":2227,
"user_id":441,
"grades":
{"html_url":"https://...",
"current_score":91.26,
"current_grade":null,
},
"sis_account_id":"11",
"user":
{"id":441,
"name":"Nicholas Bailey",
}
},
Here are the classes:
public class Grade
{
public string html_url { get; set; }
public decimal current_score { get; set; }
public string current_grade { get; set; }
}
public class User
{
public int id { get; set; }
public string name { get; set; }
}
public class Enrollment
{
public int id { get; set; }
public int user_id { get; set; }
public Grade grades { get; set; }
public string sis_account_id { get; set; }
public User user { get; set; }
}
public class RootObject
{
public Enrollment enrollment { get; set; }
}
And here is my code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://(some uri)");
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string content = sr.ReadToEnd();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
var outObject = JsonConvert.DeserializeObject<List<RootObject>>(content, settings);
I have been looking at a lot of code on google but have not found a situation that is close to mine. Also, I'm a bit green on generic collections. The code runs fine but in the debugger the "outObject" contains 10 entries, each with an "Enrollment" object that is null.
I also have tried this with json that has well over 10 entries, but it still comes up with 10. But I'm more focused on the nulls at this point (One thing at a time!). For the sake of brevity I have removed several entries in the json, but the main pattern (an enrollment object with a grades object and a user object embedded in it) remains.
Any help would be much appreciated. Thanks.
Deserialize to List<Enrollment> not List<RootObject>:
var enrollment = JsonConvert.DeserializeObject<List<Enrollment>>(content, settings);
The first JSON object in the root array has properties "id", "user_id" and so on that correspond to the members of Enrollment. Conversely there is no property "enrollment" in the JSON anywhere.
Sample fiddle.
Update
You asked, I tried this and now I'm getting an exception "Error converting value {null} to type 'System.Int32'. Path '[0].associated_user_id', line 1, position 165." associated_user_id was removed for brevity but I will add it back into my code. Any ideas?
Somewhere your data model you must have the following member (field or property):
public int associated_user_id;
Change this to a nullable:
public int? associated_user_id;

Omitting elementwrappers and getting the xml root value

<root xmlns:test="url" test:attr1="10" test:attr2="someValue>
<elementwrapper>
<secondElementwrapper>
<element>someValue</element>
<differentelement>anotherValue</differentelement>
</secondElementwrapper>
</elementwrapper>
</root>
The following classes are made:
public class XmlEntities
{
[XmlRoot("root")]
public class Root
{
[XmlElement("elementwrapper")]
public Elementwrapper Elementwrapper{ get; set; }
}
public class Elementwrapper
{
[XmlElement("secondElementwrapper")]
public SecondElementwrapper SecondElementwrapper{ get; set; }
}
public class Values
{
[XmlElement("element")]
public string Element{ get; set; }
[XmlElement("differentelement")]
public string Differentelement{ get; set; }
}
}
And here is where i serialize and deserialize the xml:
var reader = XmlReader.Create(url);
var xmlRecord = new XmlEntities.Root();
try
{
var serializer = new XmlSerializer(typeof(XmlEntities.Root));
xmlRecord = (XmlEntities.Root)serializer.Deserialize(reader);
reader.Close();
}
catch (Exception e) { }
I want to access xmlRecord.Element instead of xmlRecord.Elementwrapper.Values.Element
How do i get the test:attr1 value?*
If i remove the class Elementwrapper and Root, xmlRecord returns null.
[XmlAttribute] inside the Root class to get attr1 value doesnt work for me.
Thank you!
EDIT:
The element was a copy/paste wrong doing. Fixed that. I also added :test infront of the attr1, forgot that.
EDIT:
Adding the following as sgk mentioned, inside the root class, allowed me to access the attribute
[XmlAttribute("attr1", Namespace = "url")]
public string attr { get; set; }
EDIT: And is there a way to map the classes differently? So i can access xmlRecord.Element directly?
EDIT: #TonyStark seems like what i want needs to be approached a different way, but then again this already works i just need to access the element trough the nodes (xmlRecord.elementwrapper.secondelementwrapper.element) for those that are wondering.
To access the attribute of root i simply use : xmlRecord.attr after i added the xmlattribute that its written above.
I want to access xmlRecord.Element instead of xmlRecord.Elementwrapper.Values.Element - Based on the XML structure you have, to Deserialize and access the <element> value and <differentelement> value - you need to go through root-->elementwrapper-->secondElementwrapper.
How do i get the attr1 value? - add [XmlAttribute("attr1")] inside class Root
Then, [XmlElement("<elementwrapper>")] should be [XmlElement("elementwrapper")] otherwise when you Deserialize you will always get null as there is no matching element.
See below
public class XmlEntities
{
[XmlRoot("root")]
public class Root
{
[XmlElement("elementwrapper")]
public Elementwrapper Elementwrapper { get; set; }
[XmlAttribute("attr1", Namespace="url")]
public string attr1;
}
public class Elementwrapper
{
[XmlElement("secondElementwrapper")]
public SecondElementwrapper SecondElementwrapper { get; set; }
}
public class SecondElementwrapper
{
[XmlElement("element")]
public string Element { get; set; }
[XmlElement("differentelement")]
public string Differentelement { get; set; }
}
}

C# serialize simple object to xml. no namespace no Nulls

I got the following code:
public class Alarm:IDisposable
{
MemoryStream _tmp;
readonly XmlDocument _doc;
private readonly XmlSerializerNamespaces _ns;
private readonly XmlSerializer _x = new XmlSerializer(typeof(Alarm));
public int? ID { get; set; }
public string SourceSystem { get; set; }
public string SensorName { get; set; }
public string ModelName { get; set; }
public int? Severity { get; set; }
public int? Duration { get; set; }
public bool? Status { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public Alarm()
{
_tmp = new MemoryStream();
_doc = new XmlDocument();
_ns = new XmlSerializerNamespaces();
_ns.Add("", "");
}
public string OuterXml()
{
//Add an empty namespace and empty value
_x.Serialize(_tmp, this, _ns);
_tmp.Position = 0;
_doc.Load(_tmp);
return _doc.OuterXml;
}
public void Dispose()
{
if (_tmp!=null)
{
_tmp.Close();
_tmp = null;
}
}
}
I get as output"
<?xml version=\"1.0\"?>
<Alarm><ID>1</ID><SourceSystem>HMer</SourceSystem>
<SensorName>4</SensorName><Severity d2p1:nil=\"true\" xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" />
<Duration>500</Duration><Status>true</Status>
<StartTime>2011-07-19T12:29:51.171875+03:00</StartTime>
<EndTime d2p1:nil=\"true\"
xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" /></Alarm>
I wanna get:
<Alarm><ID>1</ID><SourceSystem>HMer</SourceSystem><SensorName>4</SensorName>
<Duration>500</Duration><Status>true</Status>
<StartTime>2011-07-19T12:29:51.171875+03:00</StartTime></Alarm>
meaning no xmlns stuff, no tag where value is null.
please assist meh
Add the following properties to your Nullable fields:
[XmlIgnore]
public bool EndTimeSpecified { get { return EndTime.HasValue; } }
This lets XmlSerializer (which is a bit dumb about Nulllable fields) realize that no EndTime has been specified, and, thus, the field does not need to be serialized.
Here's the documentation of this feature:
Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName".
Here's a related SO question that offers more details and explanations:
How to make a value type nullable with .NET XmlSerializer?
About removing the XML processing instruction (i.e., the <?xml ... ?> part), there's also a SO question about that:
Omitting XML processing instruction when serializing an object

Categories