代码之家  ›  专栏  ›  技术社区  ›  Jean-Bernard Pellerin

DataContractSerializer未反序列化所有变量

  •  4
  • Jean-Bernard Pellerin  · 技术社区  · 14 年前

    我正在尝试反序列化一些xml,而不使用用于在xml中创建对象的原始类。这个类称为compclientconfiguration。
    它成功地设置了ServerUrl和ServerUrlHda值,但其他值都没有。。。
    所以我要问的是:我如何才能正确设置其余的值,为什么它们不能使用我当前的代码。

    这是我的反序列化代码:
    conf是一个表示ComClientConfiguration xml的XElement

    DataContractSerializer ser = new DataContractSerializer(typeof(ComClientConfiguration), new Type[] {typeof(ComClientConfiguration), typeof(ComOpcClientConfiguration) });
    ComOpcClientConfiguration config = (ComOpcClientConfiguration)ser.ReadObject(conf.CreateReader());
    

    我不知道为什么我必须有ComClientConfiguration和ComClientConfiguration,可能有更好的方法来完成已知类型的hack。但现在是我的了。

    这是文件中的xml。

    <ComClientConfiguration  xsi:type="ComOpcClientConfiguration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <ServerUrl>The url</ServerUrl>
       <ServerName>a server name   </ServerName>
       <ServerNamespaceUrl>a namespace url</ServerNamespaceUrl> 
       <MaxReconnectWait>5000</MaxReconnectWait> 
       <MaxReconnectAttempts>0</MaxReconnectAttempts> 
       <FastBrowsing>true</FastBrowsing>
       <ItemIdEqualToName>true</ItemIdEqualToName>
       <ServerUrlHda>hda url</ServerUrlHda>
     </ComClientConfiguration>
    

    下面是我为反序列化而构建的类:

    [DataContract(Name = "ComClientConfiguration", Namespace = "http://opcfoundation.org/UA/SDK/COMInterop")]
    public class ComClientConfiguration
    {
        public ComClientConfiguration() { }
    
        //Prog-ID for DA-connection
        [DataMember(Name = "ServerUrl"), OptionalField]
        public string ServerUrl;//url
        [DataMember(Name = "ServerName")]
        public string ServerName;
        [DataMember(Name = "ServerNamespaceUrl")]
        public string ServerNamespaceUrl;//url
        [DataMember(Name = "MaxReconnectWait")]
        public int MaxReconnectWait;
        [DataMember(Name = "MaxReconnectAttempts")]
        public int MaxReconnectAttempts;
        [DataMember(Name = "FastBrowsing")]
        public bool FastBrowsing;
        [DataMember(Name = "ItemIdEqualToName")]
        public bool ItemIdEqualToName;
    
        //ProgID for DA-connection
        [DataMember, OptionalField]
        public string ServerUrlHda;//url
    }
    

    另外,我还必须创建这个类,它是相同的,但名称不同。用于序列化程序中的已知类型,因为我不知道整个类型命名的工作原理。

    [DataContract(Name = "ComOpcClientConfiguration", Namespace = "http://opcfoundation.org/UA/SDK/COMInterop")]
    public class ComOpcClientConfiguration
    {
        public ComOpcClientConfiguration() { }
    
        ... Same innards as ComClientConfiguration
    }
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Marc Gravell    14 年前

    数据协定序列化程序是。。。挑剔。特别是,我想知道这里的问题是否仅仅是元素顺序。然而,它也不一定是处理XML的最佳工具。XmlSerializer在这里可能更健壮—它可以处理更好的XML范围。跟单信用证根本就不是这样的 初级的 目标。

    对于简单的XML,您通常甚至不需要任何属性等xsd.exe文件在现有的XML上生成匹配的c#类(分两步;XML到xsd;xsd到c#)。

        2
  •  2
  •   Cœur N0mi    10 年前

    若要获取所有值,请尝试对顺序进行硬编码(否则可能尝试按字母顺序排列):

    [DataMember(Name = "ServerUrl", Order = 0)]
    ..
    [DataMember(Name = "ServerName", Order = 1)]
    ..
    [DataMember(Name = "ServerNamespaceUrl", Order = 2)]
    ..
    [DataMember(Name = "MaxReconnectWait", Order = 3)]
    ..