亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 SGen 為映射到兩級數組的 XML 模式生成序列化程序集?

如何使用 SGen 為映射到兩級數組的 XML 模式生成序列化程序集?

C#
慕標5832272 2023-09-09 16:26:21
我正在使用需要 XmlSerializerFormat 合約的第三方服務;我想通過創建序列化程序集來加快啟動速度,但 Sgen.exe 確實不喜歡 Xsd.exe 為其吐出嵌套數組的架構中的特定構造。該模式包括嵌套兩層深度的元素序列,如下所示:Foo.xsd<xs:schema targetNamespace="http://example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com" elementFormDefault="qualified">    <xs:element name="Foo" type="Foo"/>    <xs:complexType name="Foo">        <xs:sequence>            <xs:element name="List" type="FooList" minOccurs="0" maxOccurs="unbounded"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="FooList">        <xs:sequence>            <xs:element name="Item" type="FooListItem" minOccurs="0" maxOccurs="unbounded"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="FooListItem">        <xs:simpleContent>            <xs:extension base="xs:string"/>        </xs:simpleContent>    </xs:complexType></xs:schema>即:一個 toplevelFoo包含多個FooLists,aFooList包含多個FooListItem。運行xsd /c Foo.xsd會產生以下結果:Foo.csusing System.Xml.Serialization;[XmlType(Namespace="http://example.com")][XmlRoot(Namespace="http://example.com", IsNullable=false)]public partial class Foo {    private FooListItem[][] listField;    [XmlArrayItem("Item", typeof(FooListItem), IsNullable=false)]    public FooListItem[][] List {        get {            return this.listField;        }        set {            this.listField = value;        }    }}也就是說,FooList由于某種原因,不存在 for 類,而是只有一個嵌套的FooListItems 數組。然而,當我構建它并僅使用生成的 DLL 運行 Sgen.exe 時sgen /keep obj\Debug\net461\Foo.dll,會出現以下錯誤消息:錯誤CS0030:無法將類型“FooListItem []”轉換為“FooListItem”錯誤CS0029:無法將類型“FooListItem”隱式轉換為“FooListItem []”因此,Xsd.exe 和 Sgen.exe 似乎試圖實現這樣一種模式,即元素具有包含 X 項的顯式“X 列表”子元素,而無需為列表元素創建單獨的類,而僅依賴于序列化的名稱合成中間元素的性能;當列表元素本身可能重復時,這種情況就會中斷。有辦法解決這個問題嗎?就像強制 Xsd.exe 為中間元素生成一個類一樣?我想這可能是 Xsd.exe 和 Sgen.exe 中的一個實際錯誤,但這在合理的時間范圍內并不能真正幫助我。如上所述,這是第三方服務;我完全無法控制架構,并且對生成代碼的手動編輯越少越好,因為我的實際文件有數萬行長。
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

錯誤就在這一行


來自: [XmlArrayItem("Item", typeof(FooListItem), IsNullable=false)]


至:“[XmlArrayItem(“Item”,IsNullable = false)]


這是工作代碼的示例:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.Xml.Serialization;


namespace ConsoleApplication1

{

    class Program

    {

        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)

        {


            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("xs", "http://www.w3.org/2001/XMLSchema");

            namespaces.Add("", "http://example.com");


            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;


            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(Foo));


            Foo foo = new Foo()

            {

                List = new FooListItem[][] {

                    new FooListItem[] { 

                        new FooListItem() { Value = "abc"},

                        new FooListItem() { Value = "abd"},

                        new FooListItem() { Value = "abe"}

                    },

                    new FooListItem[] { 

                        new FooListItem() { Value = "bbc"},

                        new FooListItem() { Value = "bbd"},

                        new FooListItem() { Value = "bbe"}

                    },

                    new FooListItem[] { 

                        new FooListItem() { Value = "cbc"},

                        new FooListItem() { Value = "cbd"},

                        new FooListItem() { Value = "cbe"}

                    }

                }

            };


            serializer.Serialize(writer, foo, namespaces);




        }

    }

    [XmlType(Namespace = "http://example.com")]

    [XmlRoot(Namespace = "http://example.com", IsNullable = false)]

    public partial class Foo

    {


        private FooListItem[][] listField;


        [XmlArrayItem("Item", IsNullable = false)]

        public FooListItem[][] List

        {

            get

            {

                return this.listField;

            }

            set

            {

                this.listField = value;

            }

        }

    }


    [XmlType(Namespace = "http://example.com")]

    public partial class FooListItem

    {


        private string valueField;


        [XmlText]

        public string Value

        {

            get

            {

                return this.valueField;

            }

            set

            {

                this.valueField = value;

            }

        }

    }



查看完整回答
反對 回復 2023-09-09
  • 1 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號