Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将对象序列化为具有多个名称空间的字符串XML_C#_Xml_Serialization - Fatal编程技术网

C# 将对象序列化为具有多个名称空间的字符串XML

C# 将对象序列化为具有多个名称空间的字符串XML,c#,xml,serialization,C#,Xml,Serialization,我正在尝试将对象序列化为字符串 从中获取c#模型的xml具有多个名称空间: xmlns="http://www.example.org/standards/def/1" xmlns:ac="http://www.example.org/Standards/xyz/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rlc="http://www.example.org/standards/def/1" xmlns:

我正在尝试将对象序列化为字符串

从中获取c#模型的xml具有多个名称空间:

xmlns="http://www.example.org/standards/def/1" 
xmlns:ac="http://www.example.org/Standards/xyz/1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:rlc="http://www.example.org/standards/def/1" 
xmlns:def1="http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd"
我正在将其序列化为:

var deserialize = (MyType)pageDeserializer.Deserialize(reader);
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("ac", "urn:http://www.example.org/Standards/xyz/1");
namespaces.Add("rlc", "urn:http://www.example.org/standards/def/1");
namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd" Version="2013-06" xsi:schemaLocation="http://www.lol.org/standards/def/1 lol.xsd");


var str = pageDeserializer.SerializeAsUtf8<JvInsReinsurance>(deserialize, namespaces);
二,

1。课程是

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementX
{
    [XmlElement("ElementYName")]
    public ElementY[] ElementYNames { get; set; }
}


[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementY
{

    [XmlAttribute]
    public string Field1 { get; set; }

    public ElementYFieldAmountType FieldAmount { get; set; }

    public string Field2 { get; set; }


    private string field3;


/// <remarks/>
public string Field3
{
    get
    {
        return this.field3;
    }
    set
    {
        this.field3 = value;
    }
}

}

[Serializable]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class ElementYFieldAmountType
{
    public FieldAmount Amt { get; set; }
}

    [System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class FieldAmount
{

    private string _ccyField;

    private decimal valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ccy
    {
        get
        {
            return this._ccyField;
        }
        set
        {
            this._ccyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public decimal Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

它们看起来不错,有什么问题吗?

确保你的大写字母是正确的。在某些情况下,你有“标准”,而在其他情况下,你有“标准”。见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ElementX elementX = new ElementX()
            {
                ElementYNames = new ElementY[] {
                    new ElementY() {
                        FieldAmount =  new ElementYFieldAmountType() {
                            Amt = new FieldAmount() {
                               Ccy = "VALUR",
                               Value = 123.456M
                            }
                        },
                        Field1 = "a",
                        Field2 = "b",
                        Field3 = "c"
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(ElementX));
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("ac", "http://www.example.org/Standards/xyz/1");
            namespaces.Add("rlc", "http://www.example.org/Standards/def/1");
            namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd");

            string xml = Test.SerializeAsUtf8<ElementX>(serializer, elementX, namespaces);

        }

    }
    public static class Test
    {
        public static string SerializeAsUtf8<T>(this XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            StringWriter writer = new StringWriter();
            using (XmlWriter xWriter = XmlWriter.Create(writer, settings))
            {
                serializer.Serialize(xWriter, o, ns);
                return writer.ToString();
            }
        }
    }

    [XmlRoot(Namespace = "http://www.example.org/Standards/def/1")]
    public partial class ElementX
    {
        [XmlElement("ElementYName")]
        public ElementY[] ElementYNames { get; set; }

    }


    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public partial class ElementY
    {

        [XmlAttribute]
        public string Field1 { get; set; }

        public ElementYFieldAmountType FieldAmount { get; set; }

        public string Field2 { get; set; }


        private string field3;


        /// <remarks/>
        public string Field3
        {
            get
            {
                return this.field3;
            }
            set
            {
                this.field3 = value;
            }
        }

    }

    [Serializable]
    [XmlRoot("code")]
    [XmlType(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class ElementYFieldAmountType
    {
        public FieldAmount Amt { get; set; }
    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class FieldAmount
    {

        private string _ccyField;

        private decimal valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Ccy
        {
            get
            {
                return this._ccyField;
            }
            set
            {
                this._ccyField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public decimal Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序2
{
班级计划
{
静态void Main(字符串[]参数)
{
ElementX ElementX=新的ElementX()
{
ElementYNames=新的ElementY[]{
新元素(){
FieldAmount=new ElementyFieldDaMountType(){
金额=新字段金额(){
Ccy=“VALUR”,
值=123.456M
}
},
Field1=“a”,
Field2=“b”,
Field3=“c”
}
}
};
XmlSerializer serializer=新的XmlSerializer(typeof(ElementX));
var namespace=新的XmlSerializerNamespaces();
名称空间。添加(“ac”http://www.example.org/Standards/xyz/1");
名称空间。添加(“rlc”http://www.example.org/Standards/def/1");
名称空间。添加(“def1”http://www.lol.com/Standards/lol.xsd");
string xml=Test.SerializeAsUtf8(序列化程序、elementX、命名空间);
}
}
公共静态类测试
{
公共静态字符串SerializeAsUtf8(此XmlSerializer序列化程序,T o,XmlSerializerNamespaces ns)
{
XmlWriterSettings=新的XmlWriterSettings();
settings.Indent=true;
StringWriter编写器=新的StringWriter();
使用(XmlWriter xWriter=XmlWriter.Create(writer,设置))
{
serializer.Serialize(xWriter,o,ns);
返回writer.ToString();
}
}
}
[XmlRoot(命名空间=”http://www.example.org/Standards/def/1")]
公共部分类ElementX
{
[XmlElement(“ElementYName”)]
公共元素[]元素名称{get;set;}
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.example.org/Standards/xyz/1")]
公共部分类元素
{
[XmlAttribute]
公共字符串字段1{get;set;}
public elementyFieldDamountType字段金额{get;set;}
公共字符串字段2{get;set;}
私有字符串字段3;
/// 
公共字符串字段3
{
得到
{
返回此字段。字段3;
}
设置
{
此字段3=值;
}
}
}
[可序列化]
[XmlRoot(“代码”)]
[XmlType(AnonymousType=true,命名空间=”http://www.example.org/Standards/xyz/1")]
公共类ElementYFieldAmountType
{
公共字段金额{get;set;}
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=”http://www.example.org/Standards/xyz/1")]
公共类字段数量
{
私有字符串字段;
私有十进制值字段;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串Ccy
{
得到
{
将此返回。ccyField;
}
设置
{
此.ccyField=值;
}
}
/// 
[System.Xml.Serialization.XmlTextAttribute()]
公共十进制值
{
得到
{
返回此.valueField;
}
设置
{
this.valueField=值;
}
}
}
}

确保您的大写字母正确。在某些情况下,你有“标准”,而在其他情况下,你有“标准”。见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ElementX elementX = new ElementX()
            {
                ElementYNames = new ElementY[] {
                    new ElementY() {
                        FieldAmount =  new ElementYFieldAmountType() {
                            Amt = new FieldAmount() {
                               Ccy = "VALUR",
                               Value = 123.456M
                            }
                        },
                        Field1 = "a",
                        Field2 = "b",
                        Field3 = "c"
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(ElementX));
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("ac", "http://www.example.org/Standards/xyz/1");
            namespaces.Add("rlc", "http://www.example.org/Standards/def/1");
            namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd");

            string xml = Test.SerializeAsUtf8<ElementX>(serializer, elementX, namespaces);

        }

    }
    public static class Test
    {
        public static string SerializeAsUtf8<T>(this XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            StringWriter writer = new StringWriter();
            using (XmlWriter xWriter = XmlWriter.Create(writer, settings))
            {
                serializer.Serialize(xWriter, o, ns);
                return writer.ToString();
            }
        }
    }

    [XmlRoot(Namespace = "http://www.example.org/Standards/def/1")]
    public partial class ElementX
    {
        [XmlElement("ElementYName")]
        public ElementY[] ElementYNames { get; set; }

    }


    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public partial class ElementY
    {

        [XmlAttribute]
        public string Field1 { get; set; }

        public ElementYFieldAmountType FieldAmount { get; set; }

        public string Field2 { get; set; }


        private string field3;


        /// <remarks/>
        public string Field3
        {
            get
            {
                return this.field3;
            }
            set
            {
                this.field3 = value;
            }
        }

    }

    [Serializable]
    [XmlRoot("code")]
    [XmlType(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class ElementYFieldAmountType
    {
        public FieldAmount Amt { get; set; }
    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class FieldAmount
    {

        private string _ccyField;

        private decimal valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Ccy
        {
            get
            {
                return this._ccyField;
            }
            set
            {
                this._ccyField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public decimal Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序2
{
班级计划
{
静态void Main(字符串[]参数)
{
ElementX ElementX=新的ElementX()
{
ElementYNames=新的ElementY[]{
新元素(){
FieldAmount=new ElementyFieldDaMountType(){
金额=新字段金额(){
Ccy=“VALUR”,
值=123.456M
}
},
Field1=“a”,
Field2=“b”,
Field3=“c”
}
}
};
XmlSerializer serializer=新的XmlSerializer(typeof(ElementX));
var namespace=新的XmlSerializerNamespaces();
名称空间。添加(“ac”http://www.example.org/Standards/xyz/1");
名称空间。添加(“rlc”http://www.example.org/Standards/def/1");
名称空间。添加(“def1”http://www.lol.com/Standards/lol.xsd");
string xml=Test.SerializeAsUtf8(序列化程序、elementX、命名空间);
}
}
公共静态类测试
{
公共静态字符串SerializeAsUtf8(此XmlSerializer序列化程序,T o,XmlSerializerNamespaces ns)
{
XmlWriterSettings设置=新建XmlWriterSet
The element 'ElementX' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementY' in namespace 'http://www.example.org/standards/def/1'.
The element 'ElementP' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementQ' in namespace 'http://www.example.org/standards/def/1'.
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementX
{
    [XmlElement("ElementYName")]
    public ElementY[] ElementYNames { get; set; }
}


[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementY
{

    [XmlAttribute]
    public string Field1 { get; set; }

    public ElementYFieldAmountType FieldAmount { get; set; }

    public string Field2 { get; set; }


    private string field3;


/// <remarks/>
public string Field3
{
    get
    {
        return this.field3;
    }
    set
    {
        this.field3 = value;
    }
}

}

[Serializable]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class ElementYFieldAmountType
{
    public FieldAmount Amt { get; set; }
}

    [System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class FieldAmount
{

    private string _ccyField;

    private decimal valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Ccy
    {
        get
        {
            return this._ccyField;
        }
        set
        {
            this._ccyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public decimal Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}
<xs:complexType name="ElementX">
        <xs:sequence>
            <xs:element ref="ElementY" minOccurs="0" maxOccurs="unbounded"/>


<xs:element name="ElementY" type="ElementYType"/>
<xs:element name="FieldAmount" type="AnyAmtType"/>


<xs:complexType name="ElementYType">
        <xs:sequence>
            <xs:element ref="Field2" minOccurs="0"/>
            <xs:element ref="FieldAmount" minOccurs="0"/>
            <xs:element ref="Field3" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="Field1" type="xs:NMTOKEN" use="required"/>
    </xs:complexType>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public partial class ElementP
{
    public ElementQ ElementQName { get; set; }
}

[Serializable]
[DesignerCategory("code")]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class ElementQ
{

    public PercentageRateType Rate { get; set; }

}

    [Serializable]
[DesignerCategory("code")]
[XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/standards/def/1")]
public class PercentageRateType
{

   [XmlAttribute]
    public string RateUnit { get; set; }

    [XmlText]
    public decimal Value { get; set; }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ElementX elementX = new ElementX()
            {
                ElementYNames = new ElementY[] {
                    new ElementY() {
                        FieldAmount =  new ElementYFieldAmountType() {
                            Amt = new FieldAmount() {
                               Ccy = "VALUR",
                               Value = 123.456M
                            }
                        },
                        Field1 = "a",
                        Field2 = "b",
                        Field3 = "c"
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(ElementX));
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("ac", "http://www.example.org/Standards/xyz/1");
            namespaces.Add("rlc", "http://www.example.org/Standards/def/1");
            namespaces.Add("def1", "http://www.lol.com/Standards/lol.xsd");

            string xml = Test.SerializeAsUtf8<ElementX>(serializer, elementX, namespaces);

        }

    }
    public static class Test
    {
        public static string SerializeAsUtf8<T>(this XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            StringWriter writer = new StringWriter();
            using (XmlWriter xWriter = XmlWriter.Create(writer, settings))
            {
                serializer.Serialize(xWriter, o, ns);
                return writer.ToString();
            }
        }
    }

    [XmlRoot(Namespace = "http://www.example.org/Standards/def/1")]
    public partial class ElementX
    {
        [XmlElement("ElementYName")]
        public ElementY[] ElementYNames { get; set; }

    }


    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public partial class ElementY
    {

        [XmlAttribute]
        public string Field1 { get; set; }

        public ElementYFieldAmountType FieldAmount { get; set; }

        public string Field2 { get; set; }


        private string field3;


        /// <remarks/>
        public string Field3
        {
            get
            {
                return this.field3;
            }
            set
            {
                this.field3 = value;
            }
        }

    }

    [Serializable]
    [XmlRoot("code")]
    [XmlType(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class ElementYFieldAmountType
    {
        public FieldAmount Amt { get; set; }
    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.org/Standards/xyz/1")]
    public class FieldAmount
    {

        private string _ccyField;

        private decimal valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Ccy
        {
            get
            {
                return this._ccyField;
            }
            set
            {
                this._ccyField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public decimal Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.example.org/standards/def/1" />
  <xs:element name="ElementP" nillable="true" xmlns:q1="http://www.example.org/standards/def/1" type="q1:ElementP" />
  <xs:element name="ElementQ" nillable="true" xmlns:q2="http://www.example.org/standards/def/1" type="q2:ElementQ" />
  <xs:element name="PercentageRateType" nillable="true" xmlns:q3="http://www.example.org/standards/def/1" type="q3:PercentageRateType" />
  <xs:element name="ElementX" nillable="true" xmlns:q4="http://www.example.org/standards/def/1" type="q4:ElementX" />
  <xs:element name="ElementY" nillable="true" xmlns:q5="http://www.example.org/standards/def/1" type="q5:ElementY" />
  <xs:element name="ElementYFieldAmountType" nillable="true" xmlns:q6="http://www.example.org/standards/def/1" type="q6:ElementYFieldAmountType" />
  <xs:element name="FieldAmount" nillable="true" xmlns:q7="http://www.example.org/standards/def/1" type="q7:FieldAmount" />
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://www.example.org/standards/def/1" elementFormDefault="qualified" targetNamespace="http://www.example.org/standards/def/1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ElementP">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="ElementQName">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="1" name="Rate">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:decimal">
                    <xs:attribute name="RateUnit" type="xs:string" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ElementQ">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Rate">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:decimal">
              <xs:attribute name="RateUnit" type="xs:string" />
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="PercentageRateType">
    <xs:simpleContent>
      <xs:extension base="xs:decimal">
        <xs:attribute name="RateUnit" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="ElementX">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="ElementYName">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="1" name="FieldAmount">
              <xs:complexType>
                <xs:sequence>
                  <xs:element minOccurs="0" maxOccurs="1" name="Amt">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:decimal">
                          <xs:attribute name="Ccy" type="xs:string" />
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
            <xs:element minOccurs="0" maxOccurs="1" name="Field3" type="xs:string" />
          </xs:sequence>
          <xs:attribute name="Field1" type="xs:string" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ElementY">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="FieldAmount">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="1" name="Amt">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:decimal">
                    <xs:attribute name="Ccy" type="xs:string" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field3" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="Field1" type="xs:string" />
  </xs:complexType>
  <xs:complexType name="ElementYFieldAmountType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Amt">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:decimal">
              <xs:attribute name="Ccy" type="xs:string" />
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="FieldAmount">
    <xs:simpleContent>
      <xs:extension base="xs:decimal">
        <xs:attribute name="Ccy" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace MaPiTest
{
    public class Utf8StringWriter : StringWriter
    {
        public sealed override Encoding Encoding { get { return Encoding.UTF8; } }
    }

    class Program
    {
        public static string SerializeAsUtf8<T>(XmlSerializer serializer, T o, XmlSerializerNamespaces ns)
        {
            using (var textWriter = new Utf8StringWriter())
            {
                serializer.Serialize(textWriter, o, ns);
                return textWriter.ToString();
            }
        }

        static void Main(string[] args)
        {
            ElementX elementX = new ElementX()
            {
                ElementYNames = new ElementY[] {
                    new ElementY() {
                        FieldAmount =  new ElementYFieldAmountType() {
                            Amt = new FieldAmount() {
                               Ccy = "VALUR",
                               Value = 123.456M
                            }
                        },
                        Field1 = "a",
                        Field2 = "b",
                        Field3 = "c"
                    }
                }
            };

            // Serialize
            XmlSerializer serializer = new XmlSerializer(typeof(ElementX));
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("ac", "http://www.example.org/standards/xyz/1");
            namespaces.Add("rlc", "http://www.example.org/standards/def/1");
            namespaces.Add("def1", "http://www.lol.com/standards/lol.xsd");
            var xml = SerializeAsUtf8(serializer, elementX, namespaces);

            // Read into document.
            var doc = XDocument.Parse(xml);

            // Validate document with xsd.
            var schemas = new XmlSchemaSet();
            schemas.Add("", XmlReader.Create(new StringReader(File.ReadAllText("schema0.xsd"))));
            schemas.Add("http://www.example.org/standards/def/1", XmlReader.Create(new StringReader(File.ReadAllText("schema1.xsd"))));

            string error = null;
            doc.Validate(schemas, (o, e) => Console.WriteLine(error = e.Message));

        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ElementX xmlns:ac="http://www.example.org/standards/xyz/1" xmlns:def1="http://www.lol.com/standards/lol.xsd" xmlns:rlc="http://www.example.org/standards/def/1">
  <rlc:ElementYName Field1="a">
    <rlc:FieldAmount>
      <rlc:Amt Ccy="VALUR">123.456</rlc:Amt>
    </rlc:FieldAmount>
    <rlc:Field2>b</rlc:Field2>
    <rlc:Field3>c</rlc:Field3>
  </rlc:ElementYName>
</ElementX>
The element 'ElementX' in namespace 'urn:http://www.example.org/standards/def/1' has invalid child element 'ElementY' in namespace 'http://www.example.org/standards/def/1'.
[XmlElement(Order = 1, IsNullable = true)]
public string ElementY