通过xsd.exe将XML反序列化为生成的类

时间:2019-04-29 08:37:49

标签: c# xml

我使用xsd.exe生成了类,并且获得了40多个类:

链接:https://www.pastiebin.com/5cc3253981ff6

现在我只想反序列化广告 那是我的反序列化器:

 XmlSerializer serializer = new XmlSerializer(typeof(Adres));
 using (StringReader reader = new StringReader(xmlDocumentText))
 {
     Adres info = (Adres)(serializer.Deserialize(reader));
 }

但是我遇到了错误There is an error in XML document (1, 40)

下面只是XML的一部分(整个占用15MB):

https://www.pastiebin.com/5cc325c457a55

1 个答案:

答案 0 :(得分:0)

您的代码应如下所示

            XmlSerializer serializer = new XmlSerializer(typeof(DomainClass.wpisyWpis));
            using (StringReader reader = new StringReader(xmlDocumentText))
            {
                DomainClass.wpisyWpis info = (DomainClass.wpisyWpis)(serializer.Deserialize(reader));
            }

您还需要在此类上方添加XmlRoot:

        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [XmlRoot("wpis")]
        public partial class wpisyWpis
        {

            private Dokument dokumentField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://asseco.pl/xml/rpm/ksiegaPraktyka/2011/04/08/")]
            public Dokument Dokument
            {
                get
                {
                    return this.dokumentField;
                }
                set
                {
                    this.dokumentField = value;
                }
            }
        }