我遇到了XML对象创建的问题。我不能影响我的对象的任何值,这里有一些细节:
首先,我在C#中创建了以下XML映射:
[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; }
}
然后我发起了它:
ElaboratedData objXml = new ElaboratedData();
但问题在于:
objXml.BasicData.Type = "TravelTime";
当我试图影响obj.Xml中的某个值时,它会说:
ElaboratedData.BasicData.get返回null。
我今天在C#中发现了XML,我现在有点丢失了