处理XML序列化的有效方法 [英] Effective way of handling XML Serialization

查看:65
本文介绍了处理XML序列化的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Xsd2Code生成了Class以及后续的Serialize和Deserialize方法。


以下是使用的XSD:

<?xml version =" 1.0" encoding =" utf-8"?>< xs:schema id =" BehCons"的xmlns = QUOT;"的xmlns:XS = QUOT; HTTP://www.w3.org/2001/XMLSchema"> < xs:element name =" BehCons"> < XS:的complexType> < XS:序列> < xs:element name =" Con"的minOccurs = QUOT; 0" maxOccurs的= QUOT;无界"> < XS:的complexType> < XS:序列> < xs:element name =" Val"类型= QUOT; XS:串QUOT; /> < xs:element name =" ValByCxt"的nillable = QUOT;真"的minOccurs = QUOT; 0" maxOccurs的= QUOT;无界"> < XS:的complexType> < XS:simpleContent的> < xs:extension base =" xs:string"> < xs:attribute name =" Cl"类型= QUOT; XS:串QUOT;使用= QUOT;需" /> < xs:attribute name =" Cu"类型= QUOT; XS:串QUOT;使用= QUOT;需" /> < xs:attribute name =" Ch"类型= QUOT; XS:串QUOT;使用= QUOT;需" /> < / XS:扩展> < / XS:simpleContent的> < / XS:的complexType> < / XS:组件> < / XS:序列> < xs:attribute name =" Name"类型= QUOT; XS:串QUOT;使用= QUOT;需" /> < xs:attribute name =" Type"类型= QUOT; XS:串QUOT;使用= QUOT;需" /> < / XS:的complexType> < xs:unique name =" ContextMustBeUnique"> < xs:selector xpath =" ValByCxt" /> < xs:field xpath =" @ Cl" /> < xs:field xpath =" @ Cu" /> < xs:field xpath =" @ Ch" /> < / XS:独特> < / XS:组件> < xs:element name =" Req"> < XS:的complexType> < xs:attribute name =" Name"类型= QUOT; XS:串QUOT; /> < / XS:的complexType> < / XS:组件> < xs:element name =" Res"> < XS:的complexType> < xs:attribute name =" Name"类型= QUOT; XS:串QUOT; /> < / XS:的complexType> < / XS:组件> < / XS:序列> < / XS:的complexType> < xs:unique name =" VariableMustBeUnique"> < xs:selector xpath =" Con" /> < xs:field xpath =" @ Name" /> < / XS:独特> < / xs:element>< / xs:schema> 




生成的代码在XSD上使用Xsd2Code是:

命名空间XSD2XML {using System;使用System.Diagnostics;使用System.Xml.Serialization;使用System.Collections;使用System.Xml.Schema;使用System.ComponentModel;使用System.IO;使用System.Text;使用System.Collections.Generic; [System.CodeDom.Compiler.GeneratedCodeAttribute(" System.Xml"," 4.0.30319.1")] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code")] [System.Xml。 Serialization.XmlTypeAttribute(AnonymousType = true)] [System.Xml.Serialization.XmlRootAttribute(Namespace ="",IsNullable = false)] public partial class BehCons {private List< BehConsCon> conField;私人BehConsReq reqField;私人BehConsRes resField; private static System.Xml.Serialization.XmlSerializer serializer; public BehCons(){this.resField = new BehConsRes(); this.reqField = new BehConsReq(); this.conField = new List< BehConsCon>(); } [System.Xml.Serialization.XmlElementAttribute(" Con",Form = System.Xml.Schema.XmlSchemaForm.Unqualified,Order = 0)] public List< BehConsCon> Con {get {return this.conField; } set {this.conField = value; [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified,Order = 1)] public BehConsReq Req {get {return this.reqField; } set {this.reqField = value; [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified,Order = 2)] public BehConsRes Res {get {return this.resField; } set {this.resField = value; private static System.Xml.Serialization.XmlSerializer Serializer {get {if((serializer == null)){serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehCons)); } return serializer; #region序列化/反序列化///< summary> ///将当前BehCons对象序列化为XML文档///< / summary> ///< returns>字符串XML值< / returns> public virtual string Serialize(){System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try {memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally {if((streamReader!= null)){streamReader.Dispose(); } if((memoryStream!= null)){memoryStream.Dispose(); }}} ///< summary> ///将工作流标记反序列化为BehCons对象///< / summary> ///< param name =" xml">字符串工作流程标记,用于反序列化< / param> ///< param name =" obj">输出BehCons对象< / param> ///< param name =" exception">输出反序列化失败时的异常值< / param> ///< returns>如果此XmlSerializer可以反序列化对象,则为true;否则,假< / returns> public static bool Deserialize(string xml,out BehCons obj,out System.Exception exception){exception = null; obj = default(BehCons); try {obj = Deserialize(xml);返回true; } catch(System.Exception ex){exception = ex;返回false; public static bool Deserialize(string xml,out BehCons obj){System.Exception exception = null; return Deserialize(xml,out obj,out exception); public static BehCons Deserialize(string xml){System.IO.StringReader stringReader = null; try {stringReader = new System.IO.StringReader(xml); return((BehCons)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally {if((stringReader!= null)){stringReader.Dispose(); #endregion} [System.CodeDom.Compiler.GeneratedCodeAttribute(" System.Xml"," 4.0.30319.1")] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code") ] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class BehConsCon {private string valField;私人清单< BehConsConValByCxt> valByCxtField;私有字符串nameField;私有字符串typeField; private static System.Xml.Serialization.XmlSerializer serializer; public BehConsCon(){this.valByCxtField = new List< BehConsConValByCxt>(); } [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified,Order = 0)] public string Val {get {return this.valField; } set {this.valField = value; [System.Xml.Serialization.XmlElementAttribute(" ValByCxt",Form = System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true,Order = 1)] public List< BehConsConValByCxt> ValByCxt {get {return this.valByCxtField; } set {this.valByCxtField = value; [System.Xml.Serialization.XmlAttributeAttribute()] public string Name {get {return this.nameField; } set {this.nameField = value; [System.Xml.Serialization.XmlAttributeAttribute()] public string Type {get {return this.typeField; } set {this.typeField = value; private static System.Xml.Serialization.XmlSerializer Serializer {get {if((serializer == null)){serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsCon)); } return serializer; #region序列化/反序列化///< summary> ///将当前的BehConsCon对象序列化为XML文档///< / summary> ///< returns>字符串XML值< / returns> public virtual string Serialize(){System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try {memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally {if((streamReader!= null)){streamReader.Dispose(); } if((memoryStream!= null)){memoryStream.Dispose(); }}} ///< summary> ///将工作流标记反序列化为BehConsCon对象///< / summary> ///< param name =" xml">字符串工作流程标记,用于反序列化< / param> ///< param name =" obj">输出BehConsCon对象< / param> ///< param name =" exception">输出反序列化失败时的异常值< / param> ///< returns>如果此XmlSerializer可以反序列化对象,则为true;否则,假< / returns> public static bool Deserialize(string xml,out BehConsCon obj,out System.Exception exception){exception = null; obj = default(BehConsCon); try {obj = Deserialize(xml);返回true; } catch(System.Exception ex){exception = ex;返回false; public static bool Deserialize(string xml,out BehConsCon obj){System.Exception exception = null; return Deserialize(xml,out obj,out exception); public static BehConsCon Deserialize(string xml){System.IO.StringReader stringReader = null; try {stringReader = new System.IO.StringReader(xml); return((BehConsCon)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally {if((stringReader!= null)){stringReader.Dispose(); #endregion} [System.CodeDom.Compiler.GeneratedCodeAttribute(" System.Xml"," 4.0.30319.1")] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code") ] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class BehConsConValByCxt {private string clField;私有字符串cuField;私有字符串chField; private string valueField; private static System.Xml.Serialization.XmlSerializer serializer; [System.Xml.Serialization.XmlAttributeAttribute()] public string Cl {get {return this.clField; } set {this.clField = value; [System.Xml.Serialization.XmlAttributeAttribute()] public string Cu {get {return this.cuField; } set {this.cuField = value; [System.Xml.Serialization.XmlAttributeAttribute()] public string Ch {get {return this.chField; } set {this.chField = value; [System.Xml.Serialization.XmlTextAttribute()] public string Value {get {return this.valueField; } set {this.valueField = value; private static System.Xml.Serialization.XmlSerializer Serializer {get {if((serializer == null)){serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsConValByCxt)); } return serializer; #region序列化/反序列化///< summary> ///将当前的BehConsConValByCxt对象序列化为XML文档///< / summary> ///< returns>字符串XML值< / returns> public virtual string Serialize(){System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try {memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally {if((streamReader!= null)){streamReader.Dispose(); } if((memoryStream!= null)){memoryStream.Dispose(); }}} ///< summary> ///将工作流标记反序列化为BehConsConValByCxt对象///< / summary> ///< param name =" xml">字符串工作流程标记,用于反序列化< / param> ///< param name =" obj">输出BehConsConValByCxt对象< / param> ///< param name =" exception">输出反序列化失败时的异常值< / param> ///< returns>如果此XmlSerializer可以反序列化对象,则为true;否则,假< / returns> public static bool Deserialize(string xml,out BehConsConValByCxt obj,out System.Exception exception){exception = null; obj = default(BehConsConValByCxt); try {obj = Deserialize(xml);返回true; } catch(System.Exception ex){exception = ex;返回false; public static bool Deserialize(string xml,out BehConsConValByCxt obj){System.Exception exception = null; return Deserialize(xml,out obj,out exception); public static BehConsConValByCxt Deserialize(string xml){System.IO.StringReader stringReader = null; try {stringReader = new System.IO.StringReader(xml); return((BehConsConValByCxt)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally {if((stringReader!= null)){stringReader.Dispose(); #endregion} [System.CodeDom.Compiler.GeneratedCodeAttribute(" System.Xml"," 4.0.30319.1")] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code") ] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class BehConsReq {private string nameField; private static System.Xml.Serialization.XmlSerializer serializer; [System.Xml.Serialization.XmlAttributeAttribute()] public string Name {get {return this.nameField; } set {this.nameField = value; private static System.Xml.Serialization.XmlSerializer Serializer {get {if((serializer == null)){serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsReq)); } return serializer; #region序列化/反序列化///< summary> ///将当前的BehConsReq对象序列化为XML文档///< / summary> ///< returns>字符串XML值< / returns> public virtual string Serialize(){System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try {memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally {if((streamReader!= null)){streamReader.Dispose(); } if((memoryStream!= null)){memoryStream.Dispose(); }}} ///< summary> ///将工作流标记反序列化为BehConsReq对象///< / summary> ///< param name =" xml">字符串工作流程标记,用于反序列化< / param> ///< param name =" obj">输出BehConsReq对象< / param> ///< param name =" exception">输出反序列化失败时的异常值< / param> ///< returns>如果此XmlSerializer可以反序列化对象,则为true;否则,假< / returns> public static bool Deserialize(string xml,out BehConsReq obj,out System.Exception exception){exception = null; obj = default(BehConsReq); try {obj = Deserialize(xml);返回true; } catch(System.Exception ex){exception = ex;返回false; public static bool Deserialize(string xml,out BehConsReq obj){System.Exception exception = null; return Deserialize(xml,out obj,out exception); public static BehConsReq Deserialize(string xml){System.IO.StringReader stringReader = null; try {stringReader = new System.IO.StringReader(xml); return((BehConsReq)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally {if((stringReader!= null)){stringReader.Dispose(); #endregion} [System.CodeDom.Compiler.GeneratedCodeAttribute(" System.Xml"," 4.0.30319.1")] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code") ] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class BehConsRes {private string nameField; private static System.Xml.Serialization.XmlSerializer serializer; [System.Xml.Serialization.XmlAttributeAttribute()] public string Name {get {return this.nameField; } set {this.nameField = value; private static System.Xml.Serialization.XmlSerializer Serializer {get {if((serializer == null)){serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsRes)); } return serializer; #region序列化/反序列化///< summary> ///将当前的BehConsRes对象序列化为XML文档///< / summary> ///< returns>字符串XML值< / returns> public virtual string Serialize(){System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; try {memoryStream = new System.IO.MemoryStream(); Serializer.Serialize(memoryStream,this); memoryStream.Seek(0,System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } finally {if((streamReader!= null)){streamReader.Dispose(); } if((memoryStream!= null)){memoryStream.Dispose(); }}} ///< summary> ///将工作流标记反序列化为BehConsRes对象///< / summary> ///< param name =" xml">字符串工作流程标记,用于反序列化< / param> ///< param name =" obj">输出BehConsRes对象< / param> ///< param name =" exception">输出反序列化失败时的异常值< / param> ///< returns>如果此XmlSerializer可以反序列化对象,则为true;否则,假< / returns> public static bool Deserialize(string xml,out BehConsRes obj,out System.Exception exception){exception = null; obj = default(BehConsRes); try {obj = Deserialize(xml);返回true; } catch(System.Exception ex){exception = ex;返回false; public static bool Deserialize(string xml,out BehConsRes obj){System.Exception exception = null; return Deserialize(xml,out obj,out exception); public static BehConsRes Deserialize(string xml){System.IO.StringReader stringReader = null; try {stringReader = new System.IO.StringReader(xml); return((BehConsRes)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); } finally {if((stringReader!= null)){stringReader.Dispose(); }} #endregion}} 

我从DB获得以下结果集:


DB结果集采用文本格式(以常规'10'大小检查'Lucida Console'字体中的数据)


键入  名称   Val   Cl   Cu    Ch   Req   Res

--------------  --------------  ; --------------&NBSP; --------------&NBSP; --------------&NBSP; - -------------  --------------  --------------

Str   Key   value   NULL   NULL   NULL  

Bool   Active   MainValue  NULL   NULL  ;  NULL  

Bool   Active   newval   sur   en-US   WEB  

Bool  ;  Active   anoval     en-US   WEB  

Bool   Active   val &     cu   ch  

             Res.sur。我知道XSD的一部分。


< pre class ="prettyprint">< xs:element name =" ValByCxt"的nillable = QUOT;真"的minOccurs = QUOT; 0" maxOccurs的= QUOT;无界">
< xs:complexType>
< xs:simpleContent>
< xs:extension base =" xs:string">
< xs:attribute name =" Cl"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< xs:attribute name =" Cu"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< xs:attribute name =" Ch"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< / xs:extension>
< / xs:simpleContent>
< / xs:complexType>
< / xs:element>

而不是上面部分,它可能如下所示,这只是我的猜测。

< xs:element name =" ValByCxt"的nillable = QUOT;真"的minOccurs = QUOT; 0" maxOccurs的= QUOT;无界"> 
< xs:simpleType>
< xs:extension base =" xs:string">
< xs:attribute name =" Cl"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< xs:attribute name =" Cu"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< xs:attribute name =" Ch"类型= QUOT; XS:串QUOT;使用= QUOT;需" />
< / xs:extension>
< / xs:simpleType>
< / xs:element>


I've used Xsd2Code to generate the Class and the subsequent Methods for Serialize and Deserialize.

The following is the XSD which was used:

<?xml version="1.0" encoding="utf-8"?><xs:schema id="BehCons" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xs:element name="BehCons">    <xs:complexType>      <xs:sequence>        <xs:element name="Con" minOccurs="0" maxOccurs="unbounded">          <xs:complexType>            <xs:sequence>              <xs:element name="Val" type="xs:string"/>              <xs:element name="ValByCxt" nillable="true" minOccurs="0" maxOccurs="unbounded">                <xs:complexType>                  <xs:simpleContent>                    <xs:extension base="xs:string">                      <xs:attribute name="Cl" type="xs:string" use="required"/>                      <xs:attribute name="Cu" type="xs:string" use="required"/>                      <xs:attribute name="Ch" type="xs:string" use="required"/>                    </xs:extension>                  </xs:simpleContent>                </xs:complexType>              </xs:element>            </xs:sequence>            <xs:attribute name="Name" type="xs:string" use="required"/>            <xs:attribute name="Type" type="xs:string" use="required"/>          </xs:complexType>          <xs:unique name="ContextMustBeUnique">            <xs:selector xpath="ValByCxt"/>            <xs:field xpath="@Cl"/>            <xs:field xpath="@Cu"/>            <xs:field xpath="@Ch"/>          </xs:unique>        </xs:element>        <xs:element name="Req">          <xs:complexType>            <xs:attribute name="Name" type="xs:string" />          </xs:complexType>        </xs:element>        <xs:element name="Res">          <xs:complexType>            <xs:attribute name="Name" type="xs:string" />          </xs:complexType>        </xs:element>      </xs:sequence>    </xs:complexType>    <xs:unique name="VariableMustBeUnique">      <xs:selector xpath="Con"/>      <xs:field xpath="@Name"/>    </xs:unique>  </xs:element></xs:schema>


Code Generated by using Xsd2Code for above XSD is:

namespace XSD2XML{    using System;    using System.Diagnostics;    using System.Xml.Serialization;    using System.Collections;    using System.Xml.Schema;    using System.ComponentModel;    using System.IO;    using System.Text;    using System.Collections.Generic;    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]    [System.SerializableAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]    public partial class BehCons    {        private List<BehConsCon> conField;        private BehConsReq reqField;        private BehConsRes resField;        private static System.Xml.Serialization.XmlSerializer serializer;        public BehCons()        {            this.resField = new BehConsRes();            this.reqField = new BehConsReq();            this.conField = new List<BehConsCon>();        }        [System.Xml.Serialization.XmlElementAttribute("Con", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]        public List<BehConsCon> Con        {            get            {                return this.conField;            }            set            {                this.conField = value;            }        }        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]        public BehConsReq Req        {            get            {                return this.reqField;            }            set            {                this.reqField = value;            }        }        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 2)]        public BehConsRes Res        {            get            {                return this.resField;            }            set            {                this.resField = value;            }        }        private static System.Xml.Serialization.XmlSerializer Serializer        {            get            {                if ((serializer == null))                {                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehCons));                }                return serializer;            }        }        #region Serialize/Deserialize        /// <summary>        /// Serializes current BehCons object into an XML document        /// </summary>        /// <returns>string XML value</returns>        public virtual string Serialize()        {            System.IO.StreamReader streamReader = null;            System.IO.MemoryStream memoryStream = null;            try            {                memoryStream = new System.IO.MemoryStream();                Serializer.Serialize(memoryStream, this);                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);                streamReader = new System.IO.StreamReader(memoryStream);                return streamReader.ReadToEnd();            }            finally            {                if ((streamReader != null))                {                    streamReader.Dispose();                }                if ((memoryStream != null))                {                    memoryStream.Dispose();                }            }        }        /// <summary>        /// Deserializes workflow markup into an BehCons object        /// </summary>        /// <param name="xml">string workflow markup to deserialize</param>        /// <param name="obj">Output BehCons object</param>        /// <param name="exception">output Exception value if deserialize failed</param>        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>        public static bool Deserialize(string xml, out BehCons obj, out System.Exception exception)        {            exception = null;            obj = default(BehCons);            try            {                obj = Deserialize(xml);                return true;            }            catch (System.Exception ex)            {                exception = ex;                return false;            }        }        public static bool Deserialize(string xml, out BehCons obj)        {            System.Exception exception = null;            return Deserialize(xml, out obj, out exception);        }        public static BehCons Deserialize(string xml)        {            System.IO.StringReader stringReader = null;            try            {                stringReader = new System.IO.StringReader(xml);                return ((BehCons)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));            }            finally            {                if ((stringReader != null))                {                    stringReader.Dispose();                }            }        }        #endregion    }    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]    [System.SerializableAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class BehConsCon    {        private string valField;        private List<BehConsConValByCxt> valByCxtField;        private string nameField;        private string typeField;        private static System.Xml.Serialization.XmlSerializer serializer;        public BehConsCon()        {            this.valByCxtField = new List<BehConsConValByCxt>();        }        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]        public string Val        {            get            {                return this.valField;            }            set            {                this.valField = value;            }        }        [System.Xml.Serialization.XmlElementAttribute("ValByCxt", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true, Order = 1)]        public List<BehConsConValByCxt> ValByCxt        {            get            {                return this.valByCxtField;            }            set            {                this.valByCxtField = value;            }        }        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Name        {            get            {                return this.nameField;            }            set            {                this.nameField = value;            }        }        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Type        {            get            {                return this.typeField;            }            set            {                this.typeField = value;            }        }        private static System.Xml.Serialization.XmlSerializer Serializer        {            get            {                if ((serializer == null))                {                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsCon));                }                return serializer;            }        }        #region Serialize/Deserialize        /// <summary>        /// Serializes current BehConsCon object into an XML document        /// </summary>        /// <returns>string XML value</returns>        public virtual string Serialize()        {            System.IO.StreamReader streamReader = null;            System.IO.MemoryStream memoryStream = null;            try            {                memoryStream = new System.IO.MemoryStream();                Serializer.Serialize(memoryStream, this);                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);                streamReader = new System.IO.StreamReader(memoryStream);                return streamReader.ReadToEnd();            }            finally            {                if ((streamReader != null))                {                    streamReader.Dispose();                }                if ((memoryStream != null))                {                    memoryStream.Dispose();                }            }        }        /// <summary>        /// Deserializes workflow markup into an BehConsCon object        /// </summary>        /// <param name="xml">string workflow markup to deserialize</param>        /// <param name="obj">Output BehConsCon object</param>        /// <param name="exception">output Exception value if deserialize failed</param>        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>        public static bool Deserialize(string xml, out BehConsCon obj, out System.Exception exception)        {            exception = null;            obj = default(BehConsCon);            try            {                obj = Deserialize(xml);                return true;            }            catch (System.Exception ex)            {                exception = ex;                return false;            }        }        public static bool Deserialize(string xml, out BehConsCon obj)        {            System.Exception exception = null;            return Deserialize(xml, out obj, out exception);        }        public static BehConsCon Deserialize(string xml)        {            System.IO.StringReader stringReader = null;            try            {                stringReader = new System.IO.StringReader(xml);                return ((BehConsCon)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));            }            finally            {                if ((stringReader != null))                {                    stringReader.Dispose();                }            }        }        #endregion    }    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]    [System.SerializableAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class BehConsConValByCxt    {        private string clField;        private string cuField;        private string chField;        private string valueField;        private static System.Xml.Serialization.XmlSerializer serializer;        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Cl        {            get            {                return this.clField;            }            set            {                this.clField = value;            }        }        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Cu        {            get            {                return this.cuField;            }            set            {                this.cuField = value;            }        }        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Ch        {            get            {                return this.chField;            }            set            {                this.chField = value;            }        }        [System.Xml.Serialization.XmlTextAttribute()]        public string Value        {            get            {                return this.valueField;            }            set            {                this.valueField = value;            }        }        private static System.Xml.Serialization.XmlSerializer Serializer        {            get            {                if ((serializer == null))                {                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsConValByCxt));                }                return serializer;            }        }        #region Serialize/Deserialize        /// <summary>        /// Serializes current BehConsConValByCxt object into an XML document        /// </summary>        /// <returns>string XML value</returns>        public virtual string Serialize()        {            System.IO.StreamReader streamReader = null;            System.IO.MemoryStream memoryStream = null;            try            {                memoryStream = new System.IO.MemoryStream();                Serializer.Serialize(memoryStream, this);                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);                streamReader = new System.IO.StreamReader(memoryStream);                return streamReader.ReadToEnd();            }            finally            {                if ((streamReader != null))                {                    streamReader.Dispose();                }                if ((memoryStream != null))                {                    memoryStream.Dispose();                }            }        }        /// <summary>        /// Deserializes workflow markup into an BehConsConValByCxt object        /// </summary>        /// <param name="xml">string workflow markup to deserialize</param>        /// <param name="obj">Output BehConsConValByCxt object</param>        /// <param name="exception">output Exception value if deserialize failed</param>        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>        public static bool Deserialize(string xml, out BehConsConValByCxt obj, out System.Exception exception)        {            exception = null;            obj = default(BehConsConValByCxt);            try            {                obj = Deserialize(xml);                return true;            }            catch (System.Exception ex)            {                exception = ex;                return false;            }        }        public static bool Deserialize(string xml, out BehConsConValByCxt obj)        {            System.Exception exception = null;            return Deserialize(xml, out obj, out exception);        }        public static BehConsConValByCxt Deserialize(string xml)        {            System.IO.StringReader stringReader = null;            try            {                stringReader = new System.IO.StringReader(xml);                return ((BehConsConValByCxt)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));            }            finally            {                if ((stringReader != null))                {                    stringReader.Dispose();                }            }        }        #endregion    }    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]    [System.SerializableAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class BehConsReq    {        private string nameField;        private static System.Xml.Serialization.XmlSerializer serializer;        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Name        {            get            {                return this.nameField;            }            set            {                this.nameField = value;            }        }        private static System.Xml.Serialization.XmlSerializer Serializer        {            get            {                if ((serializer == null))                {                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsReq));                }                return serializer;            }        }        #region Serialize/Deserialize        /// <summary>        /// Serializes current BehConsReq object into an XML document        /// </summary>        /// <returns>string XML value</returns>        public virtual string Serialize()        {            System.IO.StreamReader streamReader = null;            System.IO.MemoryStream memoryStream = null;            try            {                memoryStream = new System.IO.MemoryStream();                Serializer.Serialize(memoryStream, this);                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);                streamReader = new System.IO.StreamReader(memoryStream);                return streamReader.ReadToEnd();            }            finally            {                if ((streamReader != null))                {                    streamReader.Dispose();                }                if ((memoryStream != null))                {                    memoryStream.Dispose();                }            }        }        /// <summary>        /// Deserializes workflow markup into an BehConsReq object        /// </summary>        /// <param name="xml">string workflow markup to deserialize</param>        /// <param name="obj">Output BehConsReq object</param>        /// <param name="exception">output Exception value if deserialize failed</param>        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>        public static bool Deserialize(string xml, out BehConsReq obj, out System.Exception exception)        {            exception = null;            obj = default(BehConsReq);            try            {                obj = Deserialize(xml);                return true;            }            catch (System.Exception ex)            {                exception = ex;                return false;            }        }        public static bool Deserialize(string xml, out BehConsReq obj)        {            System.Exception exception = null;            return Deserialize(xml, out obj, out exception);        }        public static BehConsReq Deserialize(string xml)        {            System.IO.StringReader stringReader = null;            try            {                stringReader = new System.IO.StringReader(xml);                return ((BehConsReq)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));            }            finally            {                if ((stringReader != null))                {                    stringReader.Dispose();                }            }        }        #endregion    }    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]    [System.SerializableAttribute()]    [System.ComponentModel.DesignerCategoryAttribute("code")]    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class BehConsRes    {        private string nameField;        private static System.Xml.Serialization.XmlSerializer serializer;        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Name        {            get            {                return this.nameField;            }            set            {                this.nameField = value;            }        }        private static System.Xml.Serialization.XmlSerializer Serializer        {            get            {                if ((serializer == null))                {                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(BehConsRes));                }                return serializer;            }        }        #region Serialize/Deserialize        /// <summary>        /// Serializes current BehConsRes object into an XML document        /// </summary>        /// <returns>string XML value</returns>        public virtual string Serialize()        {            System.IO.StreamReader streamReader = null;            System.IO.MemoryStream memoryStream = null;            try            {                memoryStream = new System.IO.MemoryStream();                Serializer.Serialize(memoryStream, this);                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);                streamReader = new System.IO.StreamReader(memoryStream);                return streamReader.ReadToEnd();            }            finally            {                if ((streamReader != null))                {                    streamReader.Dispose();                }                if ((memoryStream != null))                {                    memoryStream.Dispose();                }            }        }        /// <summary>        /// Deserializes workflow markup into an BehConsRes object        /// </summary>        /// <param name="xml">string workflow markup to deserialize</param>        /// <param name="obj">Output BehConsRes object</param>        /// <param name="exception">output Exception value if deserialize failed</param>        /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>        public static bool Deserialize(string xml, out BehConsRes obj, out System.Exception exception)        {            exception = null;            obj = default(BehConsRes);            try            {                obj = Deserialize(xml);                return true;            }            catch (System.Exception ex)            {                exception = ex;                return false;            }        }        public static bool Deserialize(string xml, out BehConsRes obj)        {            System.Exception exception = null;            return Deserialize(xml, out obj, out exception);        }        public static BehConsRes Deserialize(string xml)        {            System.IO.StringReader stringReader = null;            try            {                stringReader = new System.IO.StringReader(xml);                return ((BehConsRes)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));            }            finally            {                if ((stringReader != null))                {                    stringReader.Dispose();                }            }        }        #endregion    }}

I've the following result set from DB:

DB resultset in text format (Check this data in 'Lucida Console' font in Regular '10' size)

Type  Name  Val  Cl  Cu  Ch  Req  Res
-------------- -------------- -------------- -------------- -------------- -------------- -------------- --------------
Str  Key  value  NULL  NULL  NULL  
Bool  Active  MainValue NULL  NULL  NULL  
Bool  Active  newval  sur  en-US  WEB  
Bool  Active  anoval  sur  en-US  WEB  
Bool  Active  val  cl  cu  ch  
            Res.sur.ear Req.sur.ear

解决方案

I've doubt on a part of XSD.

<xs:element name="ValByCxt" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="Cl" type="xs:string" use="required"/>
                      <xs:attribute name="Cu" type="xs:string" use="required"/>
                      <xs:attribute name="Ch" type="xs:string" use="required"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>

instead of above part, it can be like the following, it's just my guess.

<xs:element name="ValByCxt" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:simpleType>
                    <xs:extension base="xs:string">
                      <xs:attribute name="Cl" type="xs:string" use="required"/>
                      <xs:attribute name="Cu" type="xs:string" use="required"/>
                      <xs:attribute name="Ch" type="xs:string" use="required"/>
                    </xs:extension>
                  </xs:simpleType>
              </xs:element>


这篇关于处理XML序列化的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆