Deserialize XML Node into a collection getting values from attributes - c#

if i have some xml like this
<SiteSettings>
<sortOptions>
<add name="By Date" value="date" />
<add name="By Rating" value="rating" />
</sortOptions>
</SiteSettings>
I'd like to deserialise this into an object like this
[XmlRoot("SiteSettings")]
public class SerializableSiteSettings
{
public List<NameValuePair> SortOptions { get; set; }
}
public class NameValuePair
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("value")]
public object Value { get; set; }
}
I tried adding this to the SortOptions but it did not work
[XmlArrayItem("actionButtons", typeof(List<NameValuePair>))]
public List<NameValuePair> ActionButtons { get; set; }

There are a few issues with your use of XmlSerializer:
XmlSerializer needs to know the types to expect when (de)serializing. When you have a field like public object Value { get; set; } it has no idea how to deserialize the field or what to expect therein. You need to tell it that information -- but since Value is an attribute that suggests the field is something simple, like a string. So, I'm going to assume in this answer that a string is sufficient to hold this information.
You need to tell XmlSerializer that your property named SortOptions is to be serialized with the name sortOptions, not SortOptions, using the XmlArray attribute.
You need to tell XmlSerializer that add is the name of each array element, using the XmlArrayItem attribute.
Thus, the following works and reads Value into a string:
[XmlRoot("SiteSettings")]
public class SerializableSiteSettings
{
[XmlArray("sortOptions")]
[XmlArrayItem("add", typeof(NameValuePair))]
public List<NameValuePair> SortOptions { get; set; }
}
public class NameValuePair
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
If Value is not a string and is really is some polymorphic type, you may need to implement IXmlSerializable for NameValuePair, along the lines of the following: .NET XmlSerializer: polymorphic mapping based on attribute value.

Related

XML Deserialization Error ReadElementContentAs()

I am working on deserializing the following XML document into a C# class:
<Station>
<station_id>KMSP</station_id>
<wmo_id>72658</wmo_id>
<latitude>44.88</latitude>
<longitude>-93.23</longitude>
<elevation_m>255.0</elevation_m>
<site>MINNEAPOLIS</site>
<state>MN</state>
<country>US</country>
<site_type>
<METAR />
<TAF />
</site_type>
</Station>
This is the class that it is being deserialized into:
public class Station
{
[XmlElement(ElementName ="station_id")]
public string StationId { get; set; }
[XmlElement(ElementName = "wmo_id")]
public string WmoId { get; set; }
[XmlElement(ElementName ="latitude")]
public double Latitude { get; set; }
[XmlElement(ElementName = "longtitude")]
public double Longtitude { get; set; }
[XmlElement(ElementName="site")]
public string Site { get; set; }
[XmlElement(ElementName ="state")]
public string State { get; set; }
[XmlElement(ElementName ="country")]
public string Country { get; set; }
[XmlElement(ElementName = "site_type")]
public string[] SiteType { get; set; }
}
Everything seems to be deserializng correctly, except for the <site_type> tag. The contents of this tag will vary from Station to Station, but will always contain one or more items of a specific value - essentially from an enum. These tags are always self closing and do not ever contain attributes. They are strictly in the <METAR /> format. When I attempt to deserialize this XML, I get a ReadElementContentAs() error, and I am 99% sure it's that tag that is causing the issue. Would anyone happen to know where I went wrong on this, or have any advice on how to deserialize that kind of element?
Thank you!
Taking your comment on this answer into account
For clarifcation, these type tags in the SiteType will never contain any data like the other tags. If the tag is present, it means that this weather station produces that type of report (METAR, TAF, SIGMENTS, etc)
That SiteType isn't an array of strings, but an array of types, with a different one per report type. All share the same base class.
public abstract class SiteType
{ }
public class Metar : SiteType
{ }
public class Taf : SiteType
{ }
That SiteType property then looks like below
[XmlArray(ElementName = "site_type")]
[XmlArrayItem(typeof(Metar), ElementName ="METAR")]
[XmlArrayItem(typeof(Taf), ElementName = "TAF")]
public SiteType[] SiteType { get; set; }
To check whether a certain report type is supported for a station, you check for the presence of the corresponding type within that SiteType array.
var hasMetar = p.SiteType.Any(o => o.GetType() == typeof(Metar));
You can declare a property as follows:
[XmlAnyElement(Name = "site_type")]
public XmlElement SiteType { get; set; }
After deserialization, there will be a collection of nodes in the property.
foreach(XmlNode node in station.SiteType.ChildNodes)
Console.WriteLine(node.Name);

C# XML Serialization - Custom ordering based on data [duplicate]

I have the following test XML string:
<?xml version="1.0" encoding="UTF-8"?>
<test id="myid">
<b>b1</b>
<a>a2</a>
<a>a1</a>
<b>b2</b>
</test>
which I deserialize using this class:
[XmlRoot(ElementName = "test")]
public class Test
{
[XmlElement(ElementName = "a")]
public List<string> A { get; set; }
[XmlElement(ElementName = "b")]
public List<string> B { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
If I'm now going to serialize the object the result will be:
<?xml version="1.0" encoding="utf-16"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="myid">
<a>a2</a>
<a>a1</a>
<b>b1</b>
<b>b2</b>
</test>
Is there a way to keep the initial sort order?
I guess I can't use [XmlElementAttribute(Order = x)] cause the order shouldn't be hardcoded but identically with the initial xml.
I thought about adding an order property to my lists like that
[XmlRoot(ElementName="a")]
public class A
{
[XmlAttribute(AttributeName="order")]
public string Order { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="b")]
public class B
{
[XmlAttribute(AttributeName="order")]
public string Order { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="test")]
public class Test
{
[XmlElement(ElementName="a")]
public List<A> A { get; set; }
[XmlElement(ElementName="b")]
public List<B> B { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
}
but I don't know how to order by them when serializing.
You can do this with XmlSerializer by using a single collection to capture both the <a> and <b> elements and applying the [XmlElement(Name, Type = typeof(...))] attribute to it multiple times, once for each desired element name. Because you are using a single collection to deserialize both elements, the order is automatically preserved. However, to make this work, XmlSerializer must be able to determine the correct element name when re-serializing. There are two approaches to accomplish this, as documented in Choice Element Binding Support:
If the collection contains polymorphic items, the element name can be mapped to the concrete item type by using the [XmlElementAttribute(String, Type)] constructor. For instance, if you have a sequence of elements that might be strings or integers like so:
<Things>
<string>Hello</string>
<int>999</int>
</Things>
This can be bound to a collection as follows:
public class Things
{
[XmlElement(Type = typeof(string)),
XmlElement(Type = typeof(int))]
public List<object> StringsAndInts { get; set; }
}
If the collection contains only a single type of item, the element name can be encoded in an associated array of enum values, where the enum names correspond to the element names and the array itself is identified via the [XmlChoiceIdentifierAttribute] attribute.
For details, see the documentation examples.
I find option #1 easier to work with than option #2. Using this approach, the following model will deserialize and re-serialize your XML while successfully preserving the order of the <a> and <b> elements:
public abstract class StringElementBase
{
[XmlText]
public string Text { get; set; }
public static implicit operator string(StringElementBase element)
{
return element == null ? null : element.Text;
}
}
public sealed class A : StringElementBase
{
}
public sealed class B : StringElementBase
{
}
[XmlRoot(ElementName = "test")]
public class Test
{
[XmlElement("a", Type = typeof(A))]
[XmlElement("b", Type = typeof(B))]
public List<StringElementBase> Items { get; } = new List<StringElementBase>();
[XmlIgnore]
// For convenience, enumerate through the string values of the items.
public IEnumerable<string> ItemValues { get { return Items.Select(i => (string)i); } }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
Working .Net fiddle.
For more examples of using [XmlChoiceIdentifier] to deserialize a sequence of elements with different names to c# objects of the same type, see for instance here or here.
No, basically; XmlSerializer doesn't support that. If you want to use that option you'd need to write it manually with XDocument or XmlDocument or XmlReader.

How to deserialize JSON with spaces in the attribute names?

I have a string array that I want to deserialize. Essentially, it is just a list of objects. Note that the attributes have spaces in the names:
[ { \"Event Name\": \"Hurricane Irma PR\", \"Storm Start (LST)\": \"2017-08-30\", \"Storm End (LST)\": \"2017-09-13\", \"Grid Cell Number\": 16412, \"Grid Cell State\": \"PR\", \"Grid Cell Name\": \"Grid26_0\", ...
I created a public class to template the string based on specific attributes that I want ( I don't want all the data) but I am not sure how to handle for the spaces in the names of the attributes that I want.
public class New_Events_Dataset
{
public string EventName { get; set; }
public string StormStart { get; set; }
public string StormEnd { get; set; }
public string GridCellState { get; set; }
public string GridCellName { get; set; }
public string USGSGageSiteNo { get; set; }
public string ReturnPeriodatGridCell { get; set; }
}
When I apply the deserializer with my class New_Events_Dataset like this:
var jsonResponse = returnJson.Deserialize<List<New_Events_Dataset>>(strresult);
string json = new JavaScriptSerializer().Serialize(jsonResponse);
return json;
I end up returning something like this. What am I doing wrong?
[{"EventName":null,"StormStart":null,"StormEnd":null,"GridCellState":null,"GridCellName":null,"USGSGageSiteNo":null,"ReturnPeriodatGridCell":null}
Unfortunately keys must match exactly each other.
One of the best ways to solve your problem is to define JsonProperty attribute for each property to get Deserialized object correctly. You can specify property's json key name with it.
You can take a look to this question and it's answer for better understanding:
An example of JsonProperty
Edit:
As in comments mentioned, because you are using JavaScriptSerializer JsonPropertyAttribute doesn't work in this situation.
But you can use it by adding Newtonsoft.Json Nuget Package and using it's deserilizer this way:
JsonConvert.DeserializeObject<AzureResourceData>(jsonString);

How to change XML parent tags

Say I have an xml file like so:
<ArrayOfInternetProxy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InternetProxy>
<ProxyName />
<ProxyIP>23.19.34.127:8800</ProxyIP>
<Username />
<Password />
</InternetProxy>
</ArrayOfInternetProxy>
There where many more <InternetProxy> in the xml file, but I removed them for clarity.
<InternetProxy> representes an object in my program, while <ArrayOfInternetProxy> represents an array of said object.
How can I change the tags <ArrayOfInternetProxy> and <InternetProxy> to be whatever I like?
I managed to change the attributes by using:
[XmlElement("ProxyName")]
public string ProxyName { get; set; }
But this doesn't work on the other tags.
Thanks.
My class:
public class InternetProxy //I want to change this in the xml output
{
[XmlElement("ProxyName")]
public string ProxyName { get; set; }
[XmlElement("ProxyIP")]
public string Address { get; set; }
[XmlElement("Username")]
public string UserName { get; set; }
[XmlElement("Password")]
public string Password { get; set; }
}
Edit:
Here is what I tried, why doesn't this work for me??
[XmlElement("Proxies")]
[XmlArray("Proxies")]
List<InternetProxy> proxies;
You should have something like:
[XmlArray("Proxies")]
public InternetProxy[] InternetProxy
{
get;
set;
}
To control how it's serialized you can apply following attributes:
XmlArrayAttribute: it'll change the name of the array (your actual ArrayOfInternetProxy).
XmlArrayItemAttribute: it'll change the name of the array items (your actual InternetProxy).
If you serialize directly your array (so you don't have such property with the array) you should apply XmlType attribute, like:
[XmlType(TypeName="Proxies")]
public class InternetProxy
{
}
See http://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.10).aspx for details.

C#, XML to Class DeSerialization Question

I have the following XML snippet:
<dmFiles>
−
<dmFile dmUpFileGuid="" dmFileDescr="testcase01.pdf" dmFileMetaType="main" dmFileGuid="" dmMimeType="pdf" dmFormat="">
If I create a strongly typed C# class with string properties for the dmFile attributes (eg: dmFileDescr), how can I ensure these attributes will serialize to properties in my c# class?
By using Xml attributes on your class member. Use [XmlAttribute("name")].
Your implementation would look like this:
[XmlRoot("dmFile")]
public class DmFile
{
[XmlAttribute("dmUpFileGuid")]
public String UpFileGuid { get;set; }
...
}
Try this:
[Serializable]
[XmlRoot(ElementName="dmFile")]
public class File
{
[XmlAttribute(AttributeName="dmUpFileGuid")]
public string UploadGuid { get; set; }
[XmlAttribute(AttributeName = "dmFileDescr")]
public string Description { get; set; }
[XmlAttribute(AttributeName = "dmFileMetaType")]
public string MetaType { get; set; }
[XmlAttribute(AttributeName = "dmFileGuid")]
public string FileGuid { get; set; }
[XmlAttribute(AttributeName = "dmMimeType")]
public string MimeType { get; set; }
[XmlAttribute(AttributeName = "dmFormat")]
public string Format { get; set; }
}
And deserialize your XML as follow:
XmlSerializer s = new XmlSerializer(typeof(File));
File file = s.Deserialize(new StringReader(#"<dmFile ... />")) as File;
You can (de)serialize from/to XML with the XmlSerializer and marking up the target class with Attributes provided for Xml-Serialization.
Mark your public properties with the correct attribute. It should be XmlAttributeAttribute.
The enclosing class must map on the dmFile-Element (XmlRootAttribute)
If the property is called differently, or the class is called differently than the XML element, you need to specify the XML-Element/Attribute name.

Categories

Resources