Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML反序列化丢失数组项_C#_Xml_Xsd_Deserialization - Fatal编程技术网

C# XML反序列化丢失数组项

C# XML反序列化丢失数组项,c#,xml,xsd,deserialization,C#,Xml,Xsd,Deserialization,我必须反序列化一个xml。。。这是我要做的步骤 我使用XSD xxx.xml从xml生成XSD 生成两个XSD。因此,我用xsdx1.xsdx2.xsd/classes 生成一个.cs文件,其中包含所有必要的类。我查了一下档案。一切似乎都很好。。我有一个数组,其中可以有多个结果 在我的代码中,我有一个要反序列化的xml(我用来生成xsd的xml),我用以下代码反序列化它: public static T Deserialize(Stream xml) { XmlSerializer se

我必须反序列化一个xml。。。这是我要做的步骤

  • 我使用
    XSD xxx.xml
    从xml生成XSD
  • 生成两个XSD。因此,我用
    xsdx1.xsdx2.xsd/classes
  • 生成一个
    .cs
    文件,其中包含所有必要的类。我查了一下档案。一切似乎都很好。。我有一个数组,其中可以有多个结果
  • 在我的代码中,我有一个要反序列化的xml(我用来生成xsd的xml),我用以下代码反序列化它:

    public static T Deserialize(Stream xml)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
    
        T objectDeserialized = null;
        using (StreamReader reader = new StreamReader(xml))
        {
            objectDeserialized = serializer.Deserialize(reader) as T;
        }
    
        return objectDeserialized;
    }
    
  • 我得到的是一个只有一个元素的数组,但xml有两个元素(我在调试时检查过)。。有什么问题吗

    更新

    在XML和XSD之后

    XML:


    问题是XML与XSD不匹配。XSD定义了以下层次结构:

    public partial class SELECT_CONFIGURATIONResponse
    {
         public string ReturnValue { get; set; }
         public SELECT_CONFIGURATIONResponseStoredProcedureResultSet0[] StoredProcedureResultSet0 { get; set; }
    }
    
    public partial class SELECT_CONFIGURATIONResponseStoredProcedureResultSet0
    {
        public StoredProcedureResultSet0 StoredProcedureResultSet0 { get; set }
    }
    
    public partial class StoredProcedureResultSet0
    {
        public string FlowId { get; set; }
        // Etc etc
    }
    
    也就是说,数组是类
    CONFIGURATIONResponse
    的属性,使用方式如下:

            var data = xml.LoadFromXML<SELECT_CONFIGURATIONResponse>();
    
            for (int i = 0; i < data.StoredProcedureResultSet0.Length; i++)
                Debug.WriteLine(data.StoredProcedureResultSet0[i].StoredProcedureResultSet0.FlowId);
    
    public partial class SELECT_CONFIGURATIONResponse
    {
         public string ReturnValue { get; set; }
         public SELECT_CONFIGURATIONResponseStoredProcedureResultSet0 StoredProcedureResultSet0 { get; set; }
    }
    
    public partial class SELECT_CONFIGURATIONResponseStoredProcedureResultSet0
    {
        public StoredProcedureResultSet0 StoredProcedureResultSet0[] { get; set }
    }
    
    public partial class StoredProcedureResultSet0
    {
        public string FlowId { get; set; }
        // Etc etc
    }
    
    要修复XML,需要使其与XSD匹配,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
        <SELECT_CONFIGURATIONResponse xmlns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
        <StoredProcedureResultSet0>
            <StoredProcedureResultSet0 xmlns="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION">
                <FlowId>653de913-a648-419f-85be-0e7ec93c2892</FlowId>
                <DestinationPath>C:\temp\IncomingFiles2</DestinationPath>
                <OriginPath>xxx</OriginPath>
                <DeleteAfterOperation>true</DeleteAfterOperation>
                <IntervalWatchFolder>150000000</IntervalWatchFolder>
                <SearchPattern>*.*</SearchPattern>
                <Recoverable>true</Recoverable>
                <FlowTypeId>1</FlowTypeId>
                <GatewayId>6be02fcc-ea98-4711-ab08-616a8e7be26a</GatewayId>
                <ServerAddress>sftp://127.0.0.1</ServerAddress>
                <PortNumber>22</PortNumber>
                <RetryCount>10</RetryCount>
                <Protocol_Id>1</Protocol_Id>
                <RequireAuthentication>true</RequireAuthentication>
                <RetryInterval>150000000</RetryInterval>
                <IntervalFileToRecover>150000000</IntervalFileToRecover>
                <ConfigurationId>544effad-d23c-404b-afe0-8c3c50d2fc50</ConfigurationId>
                <ServiceId>70472a23-404b-44d2-a191-d9650cdde3d6</ServiceId>
             </StoredProcedureResultSet0>
         </StoredProcedureResultSet0>
         <StoredProcedureResultSet0>
             <StoredProcedureResultSet0 xmlns="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION">
                <FlowId>73a39040-f4b2-470a-bfd2-cd09f6e91812</FlowId>
                <DestinationPath>C:\temp\IncomingFiles</DestinationPath>
                <OriginPath>Temp2</OriginPath>
                <DeleteAfterOperation>true</DeleteAfterOperation>
                <IntervalWatchFolder>150000000</IntervalWatchFolder>
                <SearchPattern>*.*</SearchPattern>
                <Recoverable>false</Recoverable>
                <FlowTypeId>1</FlowTypeId>
                <GatewayId>b6172429-8056-41cd-9db6-e0a403ba792a</GatewayId>
                <ServerAddress>ftp://127.0.0.1</ServerAddress>
                <PortNumber>21</PortNumber>
                <RetryCount>5</RetryCount>
                <Protocol_Id>2</Protocol_Id>
                <RequireAuthentication>false</RequireAuthentication>
                <RetryInterval>150000000</RetryInterval>
                <IntervalFileToRecover>150000000</IntervalFileToRecover>
                <ConfigurationId>544effad-d23c-404b-afe0-8c3c50d2fc50</ConfigurationId>
                <ServiceId>70472a23-404b-44d2-a191-d9650cdde3d6</ServiceId>
             </StoredProcedureResultSet0>
          </StoredProcedureResultSet0>
          <ReturnValue>0</ReturnValue>
    </SELECT_CONFIGURATIONResponse>
    
    
    653de913-a648-419f-85be-0e7ec93c2892
    C:\temp\IncomingFiles2
    xxx
    真的
    150000000
    *.*
    真的
    1.
    6be02fcc-ea98-4711-ab08-616a8e7be26a
    sftp://127.0.0.1
    22
    10
    1.
    真的
    150000000
    150000000
    544effad-d23c-404b-afe0-8c3c50d2fc50
    70472a23-404b-44d2-a191-d9650cdde3d6
    73a39040-f4b2-470a-bfd2-cd09f6e91812
    C:\temp\IncomingFiles
    临时2
    真的
    150000000
    *.*
    假的
    1.
    b6172429-8056-41cd-9db6-e0a403ba792a
    ftp://127.0.0.1
    21
    5.
    2.
    假的
    150000000
    150000000
    544effad-d23c-404b-afe0-8c3c50d2fc50
    70472a23-404b-44d2-a191-d9650cdde3d6
    0
    
    那么,XML不会根据发布的模式进行验证:

    ERROR: The element 'StoredProcedureResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo' has invalid child element 'StoredProcedureResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION'.
    重新生成xsd类并对代码进行细微更改(集合在层次结构中向上移动),就可以开始了。

    请显示xml和xsd生成的结果。好的,哪一个元素显示了不需要的行为?我应该有一个
    StoredProcedureResultSet0
    的列表。。。xsd看起来像是正确的
    。。。我在顶部添加了c生成的类。这是一个数组。。但最后我看到的只是xmlUff中一个元素(第一个)的数组。。。但是怎么可能。。。这是我生成XSD的方式:我使用我发布的xml。。我将其保存在一个文件(xxx.xml)中,并运行以下命令:xsd xxx.xml
            var data = xml.LoadFromXML<SELECT_CONFIGURATIONResponse>();
    
            for (int i = 0; i < data.StoredProcedureResultSet0.Length; i++)
                Debug.WriteLine(data.StoredProcedureResultSet0[i].StoredProcedureResultSet0.FlowId);
    
    public partial class SELECT_CONFIGURATIONResponse
    {
         public string ReturnValue { get; set; }
         public SELECT_CONFIGURATIONResponseStoredProcedureResultSet0 StoredProcedureResultSet0 { get; set; }
    }
    
    public partial class SELECT_CONFIGURATIONResponseStoredProcedureResultSet0
    {
        public StoredProcedureResultSet0 StoredProcedureResultSet0[] { get; set }
    }
    
    public partial class StoredProcedureResultSet0
    {
        public string FlowId { get; set; }
        // Etc etc
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
        <SELECT_CONFIGURATIONResponse xmlns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
        <StoredProcedureResultSet0>
            <StoredProcedureResultSet0 xmlns="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION">
                <FlowId>653de913-a648-419f-85be-0e7ec93c2892</FlowId>
                <DestinationPath>C:\temp\IncomingFiles2</DestinationPath>
                <OriginPath>xxx</OriginPath>
                <DeleteAfterOperation>true</DeleteAfterOperation>
                <IntervalWatchFolder>150000000</IntervalWatchFolder>
                <SearchPattern>*.*</SearchPattern>
                <Recoverable>true</Recoverable>
                <FlowTypeId>1</FlowTypeId>
                <GatewayId>6be02fcc-ea98-4711-ab08-616a8e7be26a</GatewayId>
                <ServerAddress>sftp://127.0.0.1</ServerAddress>
                <PortNumber>22</PortNumber>
                <RetryCount>10</RetryCount>
                <Protocol_Id>1</Protocol_Id>
                <RequireAuthentication>true</RequireAuthentication>
                <RetryInterval>150000000</RetryInterval>
                <IntervalFileToRecover>150000000</IntervalFileToRecover>
                <ConfigurationId>544effad-d23c-404b-afe0-8c3c50d2fc50</ConfigurationId>
                <ServiceId>70472a23-404b-44d2-a191-d9650cdde3d6</ServiceId>
             </StoredProcedureResultSet0>
         </StoredProcedureResultSet0>
         <StoredProcedureResultSet0>
             <StoredProcedureResultSet0 xmlns="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION">
                <FlowId>73a39040-f4b2-470a-bfd2-cd09f6e91812</FlowId>
                <DestinationPath>C:\temp\IncomingFiles</DestinationPath>
                <OriginPath>Temp2</OriginPath>
                <DeleteAfterOperation>true</DeleteAfterOperation>
                <IntervalWatchFolder>150000000</IntervalWatchFolder>
                <SearchPattern>*.*</SearchPattern>
                <Recoverable>false</Recoverable>
                <FlowTypeId>1</FlowTypeId>
                <GatewayId>b6172429-8056-41cd-9db6-e0a403ba792a</GatewayId>
                <ServerAddress>ftp://127.0.0.1</ServerAddress>
                <PortNumber>21</PortNumber>
                <RetryCount>5</RetryCount>
                <Protocol_Id>2</Protocol_Id>
                <RequireAuthentication>false</RequireAuthentication>
                <RetryInterval>150000000</RetryInterval>
                <IntervalFileToRecover>150000000</IntervalFileToRecover>
                <ConfigurationId>544effad-d23c-404b-afe0-8c3c50d2fc50</ConfigurationId>
                <ServiceId>70472a23-404b-44d2-a191-d9650cdde3d6</ServiceId>
             </StoredProcedureResultSet0>
          </StoredProcedureResultSet0>
          <ReturnValue>0</ReturnValue>
    </SELECT_CONFIGURATIONResponse>
    
    ERROR: The element 'StoredProcedureResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo' has invalid child element 'StoredProcedureResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION'. ERROR: The element 'SELECT_CONFIGURATIONResponse' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo' has invalid child element 'ReturnValue' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo'. List of possible elements expected: 'StoredProcedureResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo'.
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="NewDataSet" targetNamespace="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo" xmlns:mstns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo" xmlns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION">
      <xs:import namespace="http://schemas.microsoft.com/Sql/2008/05/ProceduresResultSets/dbo/SELECT_CONFIGURATION" schemaLocation="xxx_app1.xsd" />
      <xs:element name="SELECT_CONFIGURATIONResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="StoredProcedureResultSet0">
              <xs:complexType>
                <xs:sequence>
                  <xs:element ref="app1:StoredProcedureResultSet0" minOccurs="0" maxOccurs="unbounded" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="ReturnValue" type="xs:string" minOccurs="0" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="SELECT_CONFIGURATIONResponse" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>