i have an XML form look like this:
<Myset>
<element>1A</element>
<element>2B</element>
<element>3C</element>
<element>AB</element>
..........
<element>AA</element>
What should structure class, variables should i use to deserialize it. I want to read and update value of each element. I have try this, but it doesn't work:
public class Myset
{
public List<string> element {get; set;}
// or public string[] element { get; set; }
}
XmlElementAttribute will allow you specify element name.
public class case_id_list
{
[XmlElement("case_id")]
public List<string> case_id {get; set;}
}
You should use this service. It converts your xml structure to valid c# model structure. It's a very useful tool.
*In case you ever want to convert your JSON to C# this is another useful service.
Cheers!
Related
I am using C# MVC
I am importing a Json file from a location to an Rest API. In order to display the data. the program needs to know the name of the tags.
I would like to know if there is a way to get the tags from the files in an array of strings and declare them via program instead of manually inputting them.
Current C# Code
public class Person
{
public int Id { get; set; }
public int SomeID { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Current Json File:
[{"Id":212,"SomeID":71,"Number":0,"Name":"Mr Jones Mones","Address":"21, street, city"}
So as mentioned this code words fine. But, as I am importing data, the tags may contain different information and I would like to have the public declaration in a way that I can import any file to the API and be able to edit and export it.
Thank you
If you use JSON.net you can parse out json into a JObject then iterate over it's properties like so, however each property value can be any type of object so you'd need to write something to either recursively listed out nested objects or check if values are strings and only take them if they are etc.
var obj = JObject.Parse("{ \"test\": \"test\", \"test1\": \"test1\", \"test2\": \"test2\" }");
foreach (var prop in obj.Properties())
{
Console.WriteLine(prop.Name);
}
You can use JsonConvert like this:
Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Read the docs for JsonConvert for more info.
I am retrieving and successfully deserializing an xml string from a database table column for the known element names (these do not change) but there are also some nested XML Elements called "Other Attributes" which are not always going to be known. I'm having some trouble deserialising these unknown element names to a generic list so I can display them in html once deserialised.
The XML is as follows:
<Detail>
<DetailAttributes>
<Name>Name_123</Name>
<Type>Type_123</Type>
</DetailAttributes>
<OtherAttributes>
<SummaryKey AttributeName="SummaryKey">SummaryKey_123</SummaryKey>
<Account AttributeName="Account">Account_123</Account>
</OtherAttributes>
</Detail>
I have no problem deserialising the 'Name' and 'Type' elements and I can deserialise the 'SummaryKey' and 'Account' elements but only if I explicitly specify their element names - which is not the desired approach because the 'OtherAttributes' are subject to change.
My classes are as follows:
[XmlRoot("Detail")]
public class objectDetailsList
{
[XmlElement("DetailAttributes"), Type = typeof(DetailAttribute))]
public DetailAttribute[] detailAttributes { get; set; }
[XmlElement("OtherAttributes")]
public List<OtherAttribute> otherAttributes { get; set; }
public objectDetailsList()
{
}
}
[Serializable]
public class Detail Attribute
{
[XmlElement("Type")]
public string Type { get;set; }
[XmlElement("Name")]
public string Name { get;set; }
public DetailAttribute()
{
}
}
[Serializable]
public class OtherAttribute
{
//The following will deserialise ok
//[XmlElement("SummaryKey")]
//public string sumKey { get; set; }
//[XmlElement("Account")]
//public string acc { get; set; }
//What I want to do below is create a list of all 'other attributes' without known names
[XmlArray("OtherAttributes")]
public List<Element> element { get; set; }
}
[XmlRoot("OtherAttributes")]
public class Element
{
[XmlAttribute("AttributeName")]
public string aName { get; set; }
[XmlText]
public string aValue { get; set; }
}
When I try to retrieve the deserialised list of OtherAttribute elements the count is zero so it's not able to access the elements nested within "Other Attributes".
Can anybody help me with this please?
With concrete classes and dynamic data like this, you won't be able to lean on the standard XmlSerializer to serialize / deserialize for you - as it reflects on your classes, and the properties you want to populate simply don't exist. You could provide a class with all possible properties if your set of 'OtherAttributes' is known and finite, and not subject to future change, but that would give you an ugly bloated class (and I think you've already decided this is not the solution).
Practical options therefore:
Do it manually. Use the XmlDocument class, load your data with .Load(), and iterate the nodes using .SelectNodes() using an XPath query (something like "/Detail/OtherAttributes/*"). You will have to write the lot yourself, but this gives you complete control over the serialization / deserialization. You won't have to cover your code in (arguably superfluous!) attributes either.
Use Json.NET (http://james.newtonking.com/json), it allows for far greater control over serialization and deserialization. It's fast, has good docs and is overall pretty nifty really.
Deserializing a small, fixed size, fixed structure, with fixed field names, JSON string is easy: Just define a class that contains all the fields (with correct types and names, all known at compile time).
Deserializing a variable-size of repeating nested pairs, is somewhat more challenging but can be done with the help of a List<> inside the class.
But what do I do when the name of the fields are unknown at compile time? e.g.:
{
"container":{
"GD01AB491103":{
"field_id1":11,
"field_id2":12,
"field_id3":13,
"field_id4":"fourteen"
},
"DC01AB491103":{
"field_id1":21,
"field_id2":22,
"field_id3":23,
"field_id4":"twenty four"
},
"GH01AB451101":{
"field_id1":31,
"field_id2":32,
"field_id3":33,
"field_id4":"thirty four"
}
.
.
.
},
"terminator"
}
How to deserialize such a string?
(preferably with .NET's JavaScriptSerializer but if it's too weak/incapable, I might need to resort to something else)
Edit: To clarify the nature of the challenge: In the example above, in order to define a class:
public class ??????
{
public int field_id1 {get;set;}
public int field_id2 {get;set;}
public int field_id3 {get;set;}
public string field_id4 {get;set;}
}
I need to query the JSON string first, then at runtime build classes (reflection?) with these variable-name class objects in it? Looks too cumbersome... Perhaps there is a saner way?
Or maybe the class/field names are irrelevant to .NET's JavaScriptSerializer and all matters is the type? (and correct structure of course)
You can do this probably more simply than you think.. your ?????? class can be anything..
public class GenericObject
{
public int field_id1 {get;set;}
public int field_id2 {get;set;}
public int field_id3 {get;set;}
public string field_id4 {get;set;}
}
and then deserialize the root of the object graph into an object that contains a Dictionary<string,GenericObject>...
public class SomeContainer
{
public Dictionary<string,GenericObject> container {get;set;}
}
you can then loop over the values of the dictionary if you don't care about the names of the keys.
One of my clients has XML data he wants to send to a service we are creating. A sample message might look like:
<SomeRootElement>
<Dates>
<Date>2012-05-20T00:00:00</Date>
<Date>2012-05-21T00:00:00</Date>
</Dates>
</SomeRootElement>
I have a corresponding type defined as a DataContract on my WCF service application:
[DataContract]
public class SomeRootElement {
[DataMember]
public ICollection<DateTime> Dates { get; set; }
}
The trouble is that based on this definition, WCF wants the Dates collection to look like:
<Dates>
<dateTime>2012-05-20T00:00:00</dateTime>
<dateTime>2012-05-21T00:00:00</dateTime>
</Dates>
Is there any way to influence the array item's serialized element name? This used to be easy using XML serialization attributes, but the DataContractSerializer ignores those.
This should do the trick:
[DataContract]
[KnownType(typeof(DateList))]
public class SomeRootElement {
[DataMember]
public ICollection<DateTime> Dates { get; set; }
}
[CollectionDataContract(ItemName="date")]
public class DateList : Collection<DateTime> {}
You are overriding the type it can use for deserializing the ICollection for a customized one.
I'm trying to deserialize an XML that comes from a web service but I do not know how to tell the serializer how to handlke this piece of xml:
<Movimientos>
<Movimientos>
<NOM_ASOC>pI22E7P30KWB9KeUnI+JlMRBr7biS0JOJKo1JLJCy2ucI7n3MTFWkY5DhHyoPrWs</NOM_ASOC>
<FEC1>RZq60KwjWAYPG269X4r9lRZrjbQo8eRqIOmE8qa5p/0=</FEC1>
<IDENT_CLIE>IYbofEiD+wOCJ+ujYTUxgsWJTnGfVU+jcQyhzgQralM=</IDENT_CLIE>
</Movimientos>
<Movimientos>
As you can see, the child tag uses the same tag as its parent, I suppose this is wrong however the webservice is provided by an external company and that wont change it, is there any way or any library to tidy up XML or how can I use an attribute on my class so that the serializer gets it right? thanks for any help.
The serializer should be OK with it - the issue is probably just that a class can't have a property that's the same name as the class itself. So use XmlElementAttribute to match it up:
[XmlRoot("Movimientos")]
public class Movimientos
{
[XmlElement("Movimientos")]
public SomeOtherClass SomeOtherProperty { get; set; }
}
public class SomeOtherClass
{
public string NOM_ASOC { get; set; }
public string FEC1 { get; set; }
public string IDENT_CLIE { get; set; }
}
FYI, the XML is actually not wrong; there's no rule in the spec saying that a child element can't have the same name as its parent element. It's unusual maybe, but still valid.
If this is not actually deserialized by the XML serializer of .Net, it's probably better to write your own class to parse it. You can use the System.Xml or System.Xml.Linq (easier) namespaces.