【问题标题】:Deserializing XML to a type with XmlSerializer使用 XmlSerializer 将 XML 反序列化为类型
【发布时间】:2020-12-09 23:38:00
【问题描述】:

我有下面的示例 XML 和 .NET 类型,我认为这是 XMLSerializer 要使用的类型的正确属性,但我只是在我的类型中返回空值。我在不同的地方尝试了各种属性,但我无法填充类型。

[<CLIMutable>]
[<XmlTypeAttribute("ROW")>]
type MyItem =
    { Id: string
      At: string
      Latitude: double
      Longitude: double
      RegNum: string }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
type MyRowset =
    { [<XmlArrayAttribute("ROW")>]
      items: MyItem [] }

[<CLIMutable>]
[<XmlTypeAttribute("ROWSET")>]
type Myresult =
    { [<XmlElementAttribute("ROWSET")>]
      rowset: MyRowset }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
[<XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "RESPONSE")>]
type MyResponse =
    { [<XmlElementAttribute("RESULT")>]
      result: Myresult }

    
//<RESPONSE>
//    <RESULT>
//        <ROWSET>
//            <ROW>
//            </ROW>
//        </ROWSET>
//    </RESULT>
//</RESPONSE>

【问题讨论】:

    标签: c# .net xml f# xmlserializer


    【解决方案1】:

    我更新了 MyRowset 类型,现在类型如下:

    <CLIMutable>]
    [<XmlTypeAttribute("ROW")>]
    type MyItem =
        { 
          Id: string
          At: string
          Latitude: double
          Longitude: double
          RegNum: string }
    
    [<CLIMutable>]
    [<XmlTypeAttribute(AnonymousType = true)>]
    type MyRowset =
        { [<XmlElement("ROW")>]
          items: MyItem array }
    
    [<CLIMutable>]
    [<XmlTypeAttribute("ROWSET")>]
    type Myresult =
        { [<XmlElementAttribute("ROWSET")>]
          rowset: MyRowset }
    
    [<CLIMutable>]
    [<XmlTypeAttribute(AnonymousType = true)>]
    [<XmlRoot(Namespace = "", IsNullable = false, ElementName = "RESPONSE")>]
    type MyResponse =
        { [<XmlElementAttribute("RESULT")>]
          result: Myresult }
    

    XML 示例

    <RESPONSE>
        <RESULT>
            <ROWSET>
                <ROW>
                    <Id>Id1</Id>
                    <At>ATTT</At>
                    <Latitude>1.0</Latitude>
                    <Longitude>2.0</Longitude>
                    <RegNum>test</RegNum>
                </ROW>
            </ROWSET>
        </RESULT>
    </RESPONSE>
    

    输出

    result:{ result = { rowset = { items = [|{ Id = "Id1"
                                        At = "ATTT"
                                        Latitude = 1.0
                                        Longitude = 2.0
                                        RegNum = "test" }|] } } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多