反序列化只读第一项

Deserialize just read the first item

我有以下 xml 元素:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<os:FeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:hwtn="http://namespaces.os.uk/mastermap/highwaysWaterTransportNetwork/1.0" xmlns:net="http://inspire.ec.europa.eu/schemas/net/4.0" xmlns:base="http://inspire.ec.europa.eu/schemas/base/3.3" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gss="http://www.isotc211.org/2005/gss" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:gsr="http://www.isotc211.org/2005/gsr" xmlns:gn="http://inspire.ec.europa.eu/schemas/gn/4.0" xmlns:tn-w="http://inspire.ec.europa.eu/schemas/tn-w/4.0" xmlns:tn="http://inspire.ec.europa.eu/schemas/tn/4.0" xmlns:gmlxbt="http://www.opengis.net/gml/3.3/xbt" xmlns:os="http://namespaces.os.uk/product/1.0" xmlns:highway="http://namespaces.os.uk/mastermap/highwayNetwork/2.0" xmlns:base2="http://inspire.ec.europa.eu/schemas/base2/2.0" xmlns:ad="http://inspire.ec.europa.eu/schemas/ad/4.0" xmlns:au="http://inspire.ec.europa.eu/schemas/au/4.0" xmlns:bu-base="http://inspire.ec.europa.eu/schemas/bu-base/4.0" xmlns:sc="http://www.interactive-instruments.de/ShapeChange/AppInfo" xmlns:cp="http://inspire.ec.europa.eu/schemas/cp/4.0" xmlns:am="http://inspire.ec.europa.eu/schemas/am/4.0" xmlns:su-core="http://inspire.ec.europa.eu/schemas/su-core/4.0" xmlns:gmlexr="http://www.opengis.net/gml/3.3/exr" xmlns:tn-ro="http://inspire.ec.europa.eu/schemas/tn-ro/4.0" xmlns:network="http://namespaces.os.uk/mastermap/generalNetwork/2.0" xmlns:ram="http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0" xsi:schemaLocation="http://namespaces.os.uk/mastermap/highwaysWaterTransportNetwork/1.0 https://www.ordnancesurvey.co.uk/xml/schema/highwaysnetwork/1.0/HighwaysWaterTransportNetwork.xsd http://namespaces.os.uk/mastermap/highwayNetwork/2.0 https://www.ordnancesurvey.co.uk/xml/schema/highwaysnetwork/2.0/LinearHighwayNetwork.xsd http://namespaces.os.uk/product/1.0 https://www.ordnancesurvey.co.uk/xml/schema/product/1.0/OSProduct.xsd http://namespaces.os.uk/mastermap/generalNetwork/2.0 https://www.ordnancesurvey.co.uk/xml/schema/network/2.0/generalNetwork.xsd http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0 https://www.ordnancesurvey.co.uk/xml/schema/highwaysnetwork/2.0/RoutingAndAssetManagement.xsd" gml:id="OS_HIGHWAYS">
<os:metadata xlink:href="https://www.ordnancesurvey.co.uk/xml/products/Highways.xml"/>
  <os:featureMember>
    <ram:TurnRestriction gml:id="osgb4000000031265199">
      <gml:identifier codeSpace="http://inspire.jrc.ec.europa.eu/ids">http://data.os.uk/id/4000000031265199</gml:identifier>
      <net:networkRef>
        <net:LinkReference>
          <net:element xlink:href="#osgb4000000030462832"/>
          <net:applicableDirection xlink:title="in opposite direction" xlink:href="http://inspire.ec.europa.eu/codelist/LinkDirectionValue/inOppositeDirection"/>
        </net:LinkReference>
      </net:networkRef>
      <net:networkRef>
        <net:LinkReference>
          <net:element xlink:href="#osgb4000000030462831"/>
          <net:applicableDirection xlink:title="in opposite direction" xlink:href="http://inspire.ec.europa.eu/codelist/LinkDirectionValue/inOppositeDirection"/>
        </net:LinkReference>
      </net:networkRef>
      <net:inspireId>
        <base:Identifier>
          <base:localId>4000000031265199</base:localId>
          <base:namespace>http://data.os.uk/</base:namespace>
        </base:Identifier>
      </net:inspireId>
      <net:beginLifespanVersion>2016-06-17T00:00:00.000</net:beginLifespanVersion>
      <tn:validFrom nilReason="unknown" xsi:nil="true"/>
      <ram:restriction codeSpace="http://www.os.uk/xml/codelists/highways/TurnRestrictionValue.xml">No Turn</ram:restriction>
      <ram:reasonForChange codeSpace="http://www.os.uk/xml/codelists/ChangeTypeValue.xml">New</ram:reasonForChange>
    </ram:TurnRestriction>
  </os:featureMember>
</os:FeatureCollection>

而 C# class 是

[XmlArray(Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
[XmlArrayItem("LinkReference", IsNullable = false)]
public networkRefLinkReference[] networkRef {get; set;}

已由 XSD /c.

创建

但是当我使用XmlSerializer.Deserialize传输XML到对象时,它只加载一个networkRef。

我已经将第一个属性更改为以下,但它也不起作用。

[XmlArray("networkRef", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]

================== 已编辑 ====== 根据 jdweng 评论添加新的 class 结构,但仍未得到结果。

      [System.CodeDom.Compiler.GeneratedCode("xsd", "4.6.1055.0")]
[System.Serializable]
[System.Diagnostics.DebuggerStepThrough]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0")]
[XmlRoot(Namespace = "http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0", IsNullable = false)]
public class TurnRestriction
{
    [XmlElement(Namespace = "http://www.opengis.net/gml/3.2", IsNullable = true)]
    public identifier identifier { get; set; }

    [XmlElement("networkRef", IsNullable = true)]
    public NetworkRef[] NetworkRefs { get; set; }

    [XmlElement(Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
    public inspireId inspireId { get; set; }

    [XmlElement("restriction", IsNullable = true)]
    public TurnRestrictionRestriction[] Restriction { get; set; }

    public string id { get; set; }
}


 [System.CodeDom.Compiler.GeneratedCode("xsd", "4.6.1055.0")]
    [System.Serializable]
    [System.Diagnostics.DebuggerStepThrough]
    [System.ComponentModel.DesignerCategory("code")]
    [XmlType(AnonymousType = true, Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
    [XmlRoot(Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0", IsNullable = false)]
    public class networkRef
    {
        [XmlElement("LinkReference")]
        public networkRefLinkReference LinkReference { get; set; }
    }

下面的代码有效

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication49
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FeatureCollection));
            StreamReader reader = new StreamReader(FILENAME);
            FeatureCollection collection =  (FeatureCollection)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "FeatureCollection", Namespace = "http://namespaces.os.uk/product/1.0", IsNullable = false)]
    public class FeatureCollection
    {
        [XmlElement(ElementName = "metadata", Namespace = "http://namespaces.os.uk/product/1.0")]
        public Metadata metadata  { get; set;}
        [XmlElement(ElementName = "featureMember", Namespace = "http://namespaces.os.uk/product/1.0", IsNullable = false)]
        public FeatureMember featureMember { get; set; }
    }
    [XmlRoot(ElementName = "metadata", Namespace = "http://namespaces.os.uk/product/1.0", IsNullable = false)]
    public class Metadata
    {
    }
    [XmlRoot(ElementName = "featureMember", Namespace = "http://namespaces.os.uk/product/1.0", IsNullable = false)]
    public class FeatureMember
    {
        [XmlElement(ElementName = "TurnRestriction", Namespace = "http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0", IsNullable = false)]
        public TurnRestriction turnRestriction { get; set; }
    }
    [XmlRoot(ElementName = "TurnRestriction", Namespace = "http://namespaces.os.uk/mastermap/routingAndAssetManagement/2.0", IsNullable = false)]
    public class TurnRestriction
    {
        [XmlElement(ElementName = "identifier", Namespace = "http://www.opengis.net/gml/3.2", IsNullable = false)]
        public Identifier identifier { get; set; }

        [XmlElement(ElementName = "networkRef", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
        public NetworkRef[] networkRef { get; set; }
    }
    [XmlRoot(ElementName = "identifier", Namespace = "http://www.opengis.net/gml/3.2", IsNullable = false)]
    public class Identifier
    {
    }
    [XmlRoot(ElementName = "networkRef", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0", IsNullable = false)]
    public class NetworkRef
    {
        [XmlElement("LinkReference", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
        public LinkReference linkReference { get; set; }
    }
   [XmlRoot(ElementName = "LinkReference", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0", IsNullable = false)]
    public class LinkReference
    {
        [XmlElement("element", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0")]
        public Element element { get; set; }
    }    
    [XmlRoot(ElementName = "element", Namespace = "http://inspire.ec.europa.eu/schemas/net/4.0", IsNullable = false)]
    public class Element
    {
    }   

}