I've created classes from XSD Schema using xsd.exe (also tried with xsd2code which had better results in a way that they worked immediately, and with xsd.exe I have to debug some errors). XSD Schema I've used can be found at http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd , and sample file can be found at http://landxml.org/schema/LandXML-1.1/samples/AASHTO%20SDMS/MntnRoad.xml .
My code for deserialization looks like:
var mySerializer = new XmlSerializer(typeof (LandXML), new XmlRootAttribute(""));
TextReader myFileStream = new StreamReader("myFile.xml");
var myObject = (LandXML) mySerializer.Deserialize(myFileStream);
My problem is that result of deserialization is list of items of type XmlElement, so if I try to access their properties, I can't easy do that. If I want to access, for example, some Alignment object attribute in myFile.xml, the code is similar to this:
var a = myObject.Items[5];
var b = (XmlElement) a;
var c = b.ChildNodes.Item(5).ChildNodes.Item(0).ChildNodes.Item(0).Attributes[0].Value;
It is obvious that this is not a way which is meant to be while deserializing XML to classes. My idea was like (for same element):
var c = LandXML.Alignments.Alignment.CoordGeometry.Curve.rot
I don't know what I'm doing wrong, I've tried with simpler schemas, and this code was working well. Please help and tnx in advance!
EDIT 1
this is at top of my class and I think that this List type generating troubles. And there is a more similar code in my generated classes
public class LandXML
{
private List<object> _items;
private System.DateTime _date;
private System.DateTime _time;
private string _version;
private string _language;
private bool _readOnly;
private int _landXMLId;
private string _crc;
public LandXML()
{
this._items = new List<object>();
}
[System.Xml.Serialization.XmlAnyElementAttribute()]
[System.Xml.Serialization.XmlElementAttribute("Alignments", typeof(Alignments))]
[System.Xml.Serialization.XmlElementAttribute("Amendment", typeof(Amendment))]
[System.Xml.Serialization.XmlElementAttribute("Application", typeof(Application))]
[System.Xml.Serialization.XmlElementAttribute("CgPoints", typeof(CgPoints))]
[System.Xml.Serialization.XmlElementAttribute("CoordinateSystem", typeof(CoordinateSystem))]
[System.Xml.Serialization.XmlElementAttribute("FeatureDictionary", typeof(FeatureDictionary))]
[System.Xml.Serialization.XmlElementAttribute("GradeModel", typeof(GradeModel))]
[System.Xml.Serialization.XmlElementAttribute("Monuments", typeof(Monuments))]
[System.Xml.Serialization.XmlElementAttribute("Parcels", typeof(Parcels))]
[System.Xml.Serialization.XmlElementAttribute("PipeNetworks", typeof(PipeNetworks))]
[System.Xml.Serialization.XmlElementAttribute("PlanFeatures", typeof(PlanFeatures))]
[System.Xml.Serialization.XmlElementAttribute("Project", typeof(Project))]
[System.Xml.Serialization.XmlElementAttribute("Roadways", typeof(Roadways))]
[System.Xml.Serialization.XmlElementAttribute("Surfaces", typeof(Surfaces))]
[System.Xml.Serialization.XmlElementAttribute("Survey", typeof(Survey))]
[System.Xml.Serialization.XmlElementAttribute("Units", typeof(Units))]
public List<object> Items
{
get
{
return this._items;
}
set
{
this._items = value;
}
}
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(LandXML));
XmlTextReader reader = new XmlTextReader(FILENAME);
reader.Namespaces = false;
LandXML landXML = (LandXML)xs.Deserialize(reader);
}
}
[XmlRoot("LandXML")]
public class LandXML
{
[XmlAttribute("version")]
public double version { get;set; }
[XmlAttribute("date")]
public DateTime date { get;set; }
[XmlAttribute("time")]
public DateTime time { get; set; }
[XmlAttribute("readOnly")]
public Boolean readOnly { get;set; }
[XmlAttribute("language")]
public string language { get;set; }
[XmlElement("Project")]
public Project project { get; set; }
[XmlElement("Units")]
public Units units { get; set; }
[XmlElement("Application")]
public Application application { get; set; }
[XmlElement("Alignments")]
public Alignments alignments { get; set; }
}
[XmlRoot("Project")]
public class Project
{
[XmlAttribute("name")]
public string name;
}
[XmlRoot("Units")]
public class Units
{
[XmlElement("Imperial")]
public Imperial imperial { get; set; }
}
[XmlRoot("Application")]
public class Application
{
[XmlElement("Author")]
public Author author { get; set; }
}
[XmlRoot("Imperial")]
public class Imperial
{
[XmlAttribute("linearUnit")]
public string linearUnit;
[XmlAttribute("areaUnit")]
public string areaUnit;
[XmlAttribute("volumeUnit")]
public string volumeUnit;
[XmlAttribute("temperatureUnit")]
public string temperaturUnit;
[XmlAttribute("pressureUnit")]
public string pressureUnit;
[XmlAttribute("angularUnit")]
public string angularUnit;
[XmlAttribute("directionUnit")]
public string name;
}
[XmlRoot("Author")]
public class Author
{
[XmlAttribute("createdBy")]
public string createdBy;
[XmlAttribute("createdByEmail")]
public string createdByEmail;
[XmlAttribute("company")]
public string company;
[XmlAttribute("companyURL")]
public string companyURL;
}
[XmlRoot("Alignments")]
public class Alignments
{
[XmlAttribute("desc")]
public string desc;
[XmlElement("Alignment")]
public Alignment alignment { get; set; }
}
[XmlRoot("Alignment")]
public class Alignment
{
[XmlAttribute("name")]
public string name;
[XmlAttribute("desc")]
public string desc;
[XmlAttribute("length")]
public string length;
[XmlAttribute("staStart")]
public string staStart;
[XmlElement("AlignPIs")]
public AlignPIs alignPIs { get; set; }
}
[XmlRoot("AlignPIs")]
public class AlignPIs
{
[XmlElement("AlignPI")]
public List<AlignPI> alignPI { get; set; }
}
[XmlRoot("AlignPI")]
public class AlignPI
{
[XmlElement("PI")]
public PI pi { get; set; }
[XmlElement("InSpiral")]
public InSpiral inSpiral { get; set; }
[XmlElement("Curve1")]
public Curve1 cureve1 { get; set; }
[XmlElement("OutSpiral")]
public OutSpiral outSpiral { get; set; }
[XmlElement("Station")]
public Station station { get; set; }
}
[XmlRoot("Station")]
public class Station
{
[XmlText]
public string value { get; set; }
}
[XmlRoot("PI")]
public class PI
{
[XmlAttribute("code")]
public int code;
[XmlAttribute("name")]
public int name;
[XmlText]
public string value;
}
[XmlRoot("InSpiral")]
public class InSpiral
{
[XmlElement("Spiral")]
public Spiral spiral { get; set; }
}
[XmlRoot("Spiral")]
public class Spiral
{
[XmlAttribute("length")]
public double length;
[XmlAttribute("radiusEnd")]
public double radiusEnd;
[XmlAttribute("radiusStart")]
public double radiusStart;
[XmlAttribute("rot")]
public string rot;
[XmlAttribute("spiType")]
public string spiType;
}
[XmlRoot("Curve1")]
public class Curve1
{
[XmlElement("Curve")]
public Curve curve { get; set; }
}
[XmlRoot("Curve")]
public class Curve
{
[XmlAttribute("rot")]
public string rot;
[XmlAttribute("radius")]
public double radius;
}
[XmlRoot("OutSpiral")]
public class OutSpiral
{
[XmlElement("Spiral")]
public Spiral spiral { get; set; }
}
}
Related
I'd like to serialize a class construct that contains a list of up to 16 sensors ( and additional a list with up to 4 PressureSensors), which contain a list with the coefficients of their function (and some more information like ID ...).
The PressureSensor thing is additional and just shows that there are some more collections in the module. Otherwise a
List<List<SimpleSensor>>
would have been sufficient.
The XML is created without problems, only deserialization fails
<?xml version="1.0" encoding="utf-8"?>
<Module xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SingleSensors>
<SSensor>
<ID>Sensor1</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</SSensor>
<SSensor>
<ID>Sensor2</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</SSensor>
</SingleSensors>
<PressureSensors>
<PSensor>
<ID>Pressure1</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</PSensor>
<PSensor>
<ID>Pressure2</ID>
<Function>
<double>0</double>
<double>1</double>
</Function>
</PSensor>
</PressureSensors>
</Module>
The Class construct is as follows:
[Serializable]
public class SimpleSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlArray("Function")]
public Collection<double> Coefficient { get; set; }
//## Constructor:
public SimpleSensor()
{
Id = "00";
double[] content = new double[2] { 0, 1 };
Coefficient = new Collection<double>(content);
}
}
[Serializable]
[XmlRoot("Module")]
public class MostModule
{
[XmlArray("SingleSensors")]
[XmlArrayItem("SSensor")]
public List<SimpleSensor> Sensor { get; set; }
[XmlArray("PressureSensors")]
[XmlArrayItem("PSensor")]
public List<SimpleSensor> PressureSensor { get; set; }
//## Constructor:
public MostModule()
{
//Initialise SimpleSensors with 2 Sensors
SimpleSensor[] SensorArray = new SimpleSensor[2];
for (int i = 0; i < 2; i++)
{
SensorArray[i] = new SimpleSensor();
SensorArray[i].Id = "Sensor" + (i + 1);
}
Sensor = new List<SimpleSensor>(SensorArray);
PressureSensor = new List<SimpleSensor>(SensorArray);
}
public static MostModule Deserialize(string fileName)
{
MostModule Sensors;
XmlSerializer myXMLSerial = new XmlSerializer(typeof(MostModule));
using (StreamReader sr = File.OpenText(fileName))
{
Sensors = (MostModule)myXMLSerial.Deserialize(sr);
}
return Sensors;
}
When I tried to Serialize this class it works properly, but the deserialization stops with an error and the inner exception:
NotSupportedException "The collection is read-only."
I have tried the same with hard defined sensors and there is no problem in Serializing and deserializing:
public class MostModule
{
public SimpleSensor Sensor1 { get; set; }
public SimpleSensor Sensor2 { get; set; }
Is there a way to serialize the module this way, or do I have to program it all with hard defined Variables that i have to put into a list from the main?
Or is there any other suggestion for saving and loading my data in this kind of construction?
I made the following List<> objects
[XmlRoot("SingleSensors")]
public class SimpleSensor
{
[XmlElement("SSensor")]
public List<SSensor> sSensor { get; set; }
}
[XmlRoot("PressureSensors")]
public class PressureSensors
{
[XmlElement("PSensor")]
public List<SSensor> sSensor { get; set; }
}
Define SimpleSensor.Coefficient as a plain array and it will work:
[Serializable]
public class SimpleSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlArray("Function")]
public double[] Coefficient { get; set; }
//## Constructor:
public SimpleSensor()
{
Id = "00";
double[] content = new double[2] { 0, 1 };
Coefficient = content;
}
}
Try this
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication27
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(MostModule));
XmlTextReader reader = new XmlTextReader(FILENAME);
MostModule MostModule = (MostModule)xs.Deserialize(reader);
}
}
[XmlRoot("Function")]
public class Function
{
[XmlElement("double")]
public List<int> m_double {get;set;}
}
public class SSensor
{
[XmlElement("ID")]
public string Id { get; set; }
[XmlElement("Function")]
public Function Coefficient { get; set; }
}
[XmlRoot("SingleSensors")]
public class SimpleSensor
{
[XmlElement("SSensor")]
public SSensor sSensor { get; set; }
}
[XmlRoot("PressureSensors")]
public class PressureSensors
{
[XmlElement("PSensor")]
public SSensor sSensor { get; set; }
}
[XmlRoot("Module")]
public class MostModule
{
[XmlElement("SingleSensors")]
public SimpleSensor Sensor { get; set; }
[XmlElement("PressureSensors")]
public PressureSensors PressureSensor { get; set; }
}
}
I have two classes with some similar fields, some different, and a form that utilizes two different objects depending on what mode it's in (insert/edit).
Instead of using two different objects and if statements checking the form mode, I'd like to have one struct to be hydrated with either of the two objects fields so I can manipulate one object through the page life-cycle. Then separated the struct back to its respective object for insert/updating the DB.
Example of classes:
public partial class SomeClass
{
public Int32 B {get;set;}
public String C {get;set;}
public Boolean D {get;set;}
}
public class SomeOtherClass
{
public Int32 A {get;set;}
public Int32 B {get;set;}
public String C {get;set;}
}
Update with Solution Example:
public interface IInsertable
{
string SharedName { get; set; }
string SharedID { get; set; }
string editedFieldValue { get; set; }
long GetSuperSecreteInfo();
}
internal class InsertableImplementation : IInsertable
{
public string SharedName { get; set; }
public string SharedID { get; set; }
public string editedFieldValue { get; set; }
public long GetSuperSecreteInfo()
{
return -1;
}
}
public interface IUpdateable
{
string SharedName { get; set; }
string SharedID { get; set; }
string updatedFieldValue { get; set; }
Guid GenerateStevesMagicGuid();
}
internal class UpdateableImplementation : IUpdateable
{
public string SharedName { get; set; }
public string SharedID { get; set; }
public string updatedFieldValue { get; set; }
public Guid GenerateStevesMagicGuid()
{
return new Guid();
}
}
public static class WonderTwinFactory
{
public static WonderTwins GenerateWonderTwin(IUpdateable updateable, IInsertable insertable)
{
var wt = new WonderTwins();
// who will win?
wt.SharedID = updateable.SharedID;
wt.SharedID = insertable.SharedID;
// you decide?
wt.SharedName = updateable.SharedName;
wt.editedFieldValue = "stuff";
return wt;
}
}
public class WonderTwins : IInsertable, IUpdateable
{
public string SharedName { get; set; }
public string SharedID { get; set; }
public string editedFieldValue { get; set; }
public long GetSuperSecreteInfo()
{
return 1;
}
public string updatedFieldValue { get; set; }
public Guid GenerateStevesMagicGuid()
{
return new Guid();
}
}
class Program
{
static void Main(string[] args)
{
IUpdateable updateable = new UpdateableImplementation();
IInsertable insertable = new InsertableImplementation();
WonderTwins dualImplementatin = WonderTwinFactory.GenerateWonderTwin(updateable, insertable);
IUpdateable newUpdateable = dualImplementatin as IUpdateable;
IInsertable newInsertable = dualImplementatin as IInsertable;
}
}
Have both classes implement an interface that defines the operations common to each, including both the fields that are shared (assuming the view needs to access them) and also a method to actually perform the operation that they represent (insert/edit).
Other way of doing such things is using C# dynamic object and assign properties directly. It may help to avoid any new type or interface and directly utilizing new dynamic object any time, as much as required.
var newObject = new {
objectOfClass1 = x.prop1,
objectOfClass2 = x.prop2
}
I'm new on java/android:
I'm using this list of objects on wp7 and I want pass to android, how I do this:
My big object c#:
public class ListCountries
{
public List<CountriesRepresented> _countriesRepresented { get; set; }
public List<CountriesOrigin > _countriesOrigin { get; set; }
}
My others two objects in c#:
public class CountriesRepresented
{
public int CountryID { get; set; }
public string Designation { get; set; }
public string Symbol { get; set; }
public string NomDesignationISO { get; set; }
}
public class CountriesOrigin
{
public int CountryID { get; set; }
public string Designation { get; set; }
public string Symbol { get; set; }
public string NomDesignationISO { get; set; }
}
My java Deserializer:
public Object[] getListCountries()
{
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(Config.WS_PATH);
post.setHeader("content-type", "application/json; charset=UTF-8");
post.addHeader("Client-Application","3601cfde-e440-4a84-a2cc-a402f4c7bd14");
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
ListCountries _listCountries = new JSONDeserializer().deserialize(ListCountries .class, respStr);
return _listCountries;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
My big object in java:
public class ListCountries {
public List<CountriesRepresented> _CountriesRepresented;
public List<CountriesOrigin > _CountriesOrigin ;
public List<CountriesRepresented> getCountriesRepresented() {
return this._CountriesRepresented;
}
public List<CountriesOrigin > getCountriesOrigin() {
return this._CountriesOrigin ;
}
public void setCountriesRepresented (List<CountriesRepresented> CountriesRepresented) {
this._CountriesRepresented = CountriesRepresented;
}
public void setCountriesOrigin (List<CountriesOrigin > CountriesOrigin ) {
this._CountriesOrigin = CountriesOrigin ;
}
}
My service is on WebAPI and give me an correct answer example: `{"PaisesRepresentantes":[{"PaisID":4,"Designacao":"Alemanha","Sigla":"DEU","NomDesignacaoISO":"GERMANY"},{"PaisID":21,.......
your classes in Java should be something like,
public class ListCountries
{
public List<CountriesRepresented> _countriesRepresented; //Make it private if you want and then you can add getter setter function
public List<CountriesOrigin > _countriesOrigin;
}
public class CountriesRepresented
{
public int CountryID; //Make it private if you want and then you can add getter setter function
public String Designation;
public String Symbol;
public String NomDesignationISO;
}
public class CountriesOrigin
{
public int CountryID; //Make it private if you want and then you can add getter setter function
public String Designation;
public String Symbol;
public String NomDesignationISO;
}
The problem with your existing code is that you have declared variable as _CountriesRepresented rather it should be _countriesRepresented i.e. properties are case sensitive and those are mapped to variables declared in class and do remember to add a default constructor if you add any custom constructor to any of those classes
I build a rest service which output are json. I using Newtonsoft.Json.
This is my class.
public class DownloadPDA
{
public List<FRUTE> lsRute { get; set; }
public List<FCUSTMST> lsCustomer { get; set; }
public List<FMASTER> lsMaster { get; set; }
public List<FNOTEC> lsNotec { get; set; }
public List<FINFO> lsInfo { get; set; }
public List<FBRAND> lsBrand { get; set; }
public List<FKPL> lsKpl { get; set; }
}
but when I test my rest service my result are:
{"downloadDataResult":"{"lsBrand":[{}],"lsCustomer":[{},{},{}],"lsInfo":[],"lsKpl":null,"lsMaster":[{},{},{},{},{}],"lsNotec":[],"lsRute":[{},{},{}]}"}
it not show the data in list. I know something is wrong. Can anybody help?
This one of my collection class
public class FRUTE
{
private String norute;
private String custno;
private String flag;
private String st_visit;
private float amount;
private int jmlvisit;
public FRUTE() { }
public void getData(DCTRTDTO dto) {
this.norute = dto.NOROUT;
this.custno = dto.NOCUST;
this.flag = dto.FLAG;
this.st_visit = "not yet";
this.amount = 10;
this.jmlvisit = 1;
}
public static List<FRUTE> getList(List<DCTRTDTO> lsRute)
{
List<FRUTE> ls = new List<FRUTE>();
FRUTE info = new FRUTE();
foreach (DCTRTDTO dto in lsRute)
{
info.getData(dto);
ls.Add(info);
}
return ls;
}
}
Your FRUTE class doesn't have public properties that are required for Json serialization.
Encapsulate you private fields and all will work as expected.
public class FRUTE
{
private String norute;
private String custno;
public string Norute
{
get { return norute; }
set { norute = value; }
}
public string Custno
{
get { return custno; }
set { custno = value; }
}
//...
}
I have a business model which consists of a parent/child relationship (Identifier/IdentifierValue) and also some snapshot classes which look the same (IdentifierSnapshot/IdentifierValueSnapshot).
I am trying to create an extension method which will work on an enumeration of either Identifier or IdentifierSnapshot, but I just can't work out what to put in the extension method where I have inserted the XXX placeholder.
//Interfaces
public interface IIdentifier<TIdentifierValue>
where TIdentifierValue : IIdentifierValue
{
string Code { get; }
IEnumerable<TIdentifierValue> GetValues();
}
public interface IIdentifierValue
{
string Code { get; }
string Value { get; }
}
//Main classes
public class Identifier : IIdentifier<IdentifierValue>
{
public string Code { get; set; }
public IEnumerable<IdentifierValue> GetValues();
}
public class IdentifierValue : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
//Snapshots
public class IdentifierSnapshot : IIdentifier<IdentifierValueSnapshot>
{
public string Code { get; set; }
public IEnumerable<IdentifierValueSnapshot> GetValues();
}
public class IdentifierValueSnapshot : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
public static IdentifierExtensions
{
public static IEnumerable<XXX> ByCode<XXX>(this IEnumerable<XXX> instance, string code)
{
return instance.Where(x => string.Compare(code, x.Code, true) == 0);
}
}
I think this would do it:
public static IEnumerable<T> ByCode<T,Z>(this IEnumerable<T> instance, string code)
where T:IIdentifier<Z>
where Z:IIdentifierValue