0

I've encounter an issue with the XML object creation. I can't affect any value to my object, here's some detail :

First, I created the following XML mapping in C# :

[XmlRoot(ElementName = "pertinentLocation")]
public class PertinentLocation
{
    [XmlElement(ElementName = "predefinedLocationReference")]
    public string PredefinedLocationReference { get; set; }
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
}

[XmlRoot(ElementName = "travelTime")]
public class TravelTime
{
    [XmlElement(ElementName = "duration")]
    public string Duration { get; set; }
}

[XmlRoot(ElementName = "freeFlowTravelTime")]
public class FreeFlowTravelTime
{
    [XmlElement(ElementName = "duration")]
    public string Duration { get; set; }
}

[XmlRoot(ElementName = "freeFlowSpeed")]
public class FreeFlowSpeed
{
    [XmlElement(ElementName = "speed")]
    public string Speed { get; set; }
}

[XmlRoot(ElementName = "travelTimeDataExtended")]
public class TravelTimeDataExtended
{
    [XmlElement(ElementName = "reliability")]
    public string Reliability { get; set; }
}

[XmlRoot(ElementName = "travelTimeDataExtension")]
public class TravelTimeDataExtension
{
    [XmlElement(ElementName = "travelTimeDataExtended")]
    public TravelTimeDataExtended TravelTimeDataExtended { get; set; }
}

[XmlRoot(ElementName = "basicData")]
public class BasicData
{
    [XmlElement(ElementName = "measurementOrCalculatedTimePrecision")]
    public string MeasurementOrCalculatedTimePrecision { get; set; }
    [XmlElement(ElementName = "measurementOrCalculationPeriod")]
    public string MeasurementOrCalculationPeriod { get; set; }
    [XmlElement(ElementName = "measurementOrCalculationTime")]
    public string MeasurementOrCalculationTime { get; set; }
    [XmlElement(ElementName = "pertinentLocation")]
    public PertinentLocation PertinentLocation { get; set; }
    [XmlElement(ElementName = "travelTimeTrendType")]
    public string TravelTimeTrendType { get; set; }
    [XmlElement(ElementName = "travelTime")]
    public TravelTime TravelTime { get; set; }
    [XmlElement(ElementName = "freeFlowTravelTime")]
    public FreeFlowTravelTime FreeFlowTravelTime { get; set; }
    [XmlElement(ElementName = "freeFlowSpeed")]
    public FreeFlowSpeed FreeFlowSpeed { get; set; }
    [XmlElement(ElementName = "travelTimeDataExtension")]
    public TravelTimeDataExtension TravelTimeDataExtension { get; set; }
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
}

[XmlRoot(ElementName = "elaboratedData")]
public class ElaboratedData
{
    [XmlElement(ElementName = "basicData")]
    public BasicData BasicData { get; set; }
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
}

Then i've initiate it:

ElaboratedData objXml = new ElaboratedData(); 

But the problem is here :

objXml.BasicData.Type = "TravelTime";

When i try to affect a value to something in obj.Xml it say :

ElaboratedData.BasicData.get has return null.

I've discover XML in C# today, im kinda lost right now

DiSiZ
  • 43
  • 10
  • 1
    Is that the **exact** error you received? – mjwills Jun 13 '18 at 12:39
  • What happens if you use `ElaboratedData objXml = new ElaboratedData() { BasicData = new BasicData() };` instead? Also have a read of https://softwareengineering.stackexchange.com/questions/111344/what-is-it-called-when-you-set-properties-on-object-initialization . – mjwills Jun 13 '18 at 12:40
  • 1
    This has nothing to do with XML. After `objXml = new ElaboratedData()`, `objXml.BasicData` is still `null`. Initialize it. – CodeCaster Jun 13 '18 at 12:42
  • Wow mjwills it did something actually. Now it crash when i do : objXml.BasicData.PertinentLocation.Type = "LocationByReference"; And the same error but now with pertinentlocation : Xml2CSharp.BasicData.PertinentLocation.get retournée null. – DiSiZ Jun 13 '18 at 12:44
  • Oh yea okay thats it, i got id CodeCaster, thanks – DiSiZ Jun 13 '18 at 12:45
  • @DiSiZ The pattern I showed you - you need to follow it **all of the way down**. – mjwills Jun 13 '18 at 12:45
  • Yes, sure, thanks, wow it was a terrible fail – DiSiZ Jun 13 '18 at 12:47
  • It was not a terrible fail. We all learn new things every day. – mjwills Jun 13 '18 at 13:23

0 Answers0