具有多个名称空间和名称空间前缀的C#XML反序列化

时间:2018-10-09 07:03:38

标签: c# xml serialization

我很确定这已经被询问并回答了,我只是找不到

我正在尝试将XML反序列化为POCO。
这是我苦苦挣扎的部分

<Trias version="1.1" xmlns="http://www.vdv.de/trias" xmlns:siri="http://www.siri.org.uk/siri"

这是我的整个XML:

<?xml version="1.0" encoding="UTF-8"?>
<Trias version="1.1" xmlns="http://www.vdv.de/trias" xmlns:siri="http://www.siri.org.uk/siri" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ServiceRequest>
<siri:RequestTimestamp>2012-10-28T20:56:00Z</siri:RequestTimestamp>
<siri:RequestorRef>SEUS</siri:RequestorRef>
<RequestPayload>
<TripRequest>
<Origin>
<LocationRef>
<StopPointRef>8500320</StopPointRef>
</LocationRef>
<DepArrTime>2018-10-09T08:05:51</DepArrTime>
</Origin>
<Destination>
<LocationRef>
<StopPointRef>8500322</StopPointRef>
</LocationRef>
<DepArrTime>2018-10-09T08:05:51</DepArrTime>
</Destination>
<Params>
<NumberOfResults>1</NumberOfResults>
<IncludeTrackSections>true</IncludeTrackSections>
<IncludeLegProjection>true</IncludeLegProjection>
<IncludeIntermediateStops>true</IncludeIntermediateStops>
</Params>
</TripRequest>
</RequestPayload>
</ServiceRequest>
</Trias>

如何在C#中为xmlns:siri建模?我的班级看起来像这样:

[XmlRoot(nameof(Trias), Namespace = "http://www.vdv.de/trias")]
public class Trias
{

    [XmlAttribute("version")]
    public string Version { get; set; }

    [XmlAttribute("xmlns")]
    public string Xmlns { get; set; }

    [XmlAttribute("siri")]
    public string Siri { get; set; }
}

1 个答案:

答案 0 :(得分:1)

尝试将属性XmlAttribute的{​​{1}}从Siri更改为[XmlAttribute("siri")]

[XmlAttribute(Namespace = "http://www.siri.org.uk/siri")]