I have a schema defined as follows:
我有一个模式定义如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Books" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Books" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Book" type="MyBookType"></xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="MyBookType">
...
</xs:complexType>
</xs:schema>
Using this schema and xsd.exe, I generate the classes that will be used during serialization. The class generated by the above schema produces the following xml when serialized:
使用这个模式和xsd。exe,我生成将在序列化期间使用的类。上述模式生成的类在序列化时生成以下xml:
<Books>
<Book>
...
</Book>
</Books>
This xml is used in a SOAP request and the service on the other end expects the following xml:
该xml用于SOAP请求,而另一端的服务期望以下xml:
<Books>
<Book xsi:type="MyBookType">
...
</Book>
</Books>
How can I edit my schema so that the xsi:type attribute is included in the serialized xml?
如何编辑模式,使xsi:type属性包含在序列化的xml中?
1 个解决方案
#1
5
Use a derived type, and an XmlInclude
attribute. For example:
使用派生类型和XmlInclude属性。例如:
public class Book
{
public string Title;
public string Author;
}
public class MyBookType : Book { }
[XmlInclude(typeof(MyBookType))]
[XmlRoot("Books")]
public class Books : List<Book> { }
public void Run()
{
var b = new Books();
b.Add(new MyBookType
{
Title = "The Art of War",
Author = "Sun Tzu"
});
b.Add(new MyBookType
{
Title = "Great Expectations",
Author = "Charles Dickens"
});
var s = new XmlSerializer(typeof(Books));
s.Serialize(Console.Out, b);
}
Running this produces this output:
运行此命令将产生以下输出:
<?xml version="1.0" encoding="IBM437"?>
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Book xsi:type="MyBookType">
<Title>The Art of War</Title>
<Author>Sun Tzu</Author>
</Book>
<Book xsi:type="MyBookType">
<Title>Great Expectations</Title>
<Author>Charles Dickens</Author>
</Book>
</Books>
Since you're using a SOAP request, I'm assuming ASMX, which means the serialization happens implicitly. You will need to apply [XmlInclude]
to whatever holds the collection of books. This could be a parameter in a webmethod, for example.
因为您正在使用SOAP请求,所以我假设是ASMX,这意味着序列化是隐式的。您将需要将[XmlInclude]应用到包含图书集合的任何地方。例如,这可以是webmethod中的一个参数。
You can automatically generate the appropriate XmlInclude attribute, starting from XSD and WSDL, if you define the types in XSD, with the inheritance relationship I illustrated in C# code.
如果您在XSD中定义了类型,您可以从XSD和WSDL开始自动生成适当的XmlInclude属性,继承关系如c#代码中所示。
In WSDL, the request message might take a Books
type, which is a collection of Book
. Separately, define a MyBookType
which derives from Book but does not extend it.
在WSDL中,请求消息可能采用图书类型,这是图书的集合。另外,定义一个MyBookType,它派生自Book,但不扩展它。
#1
5
Use a derived type, and an XmlInclude
attribute. For example:
使用派生类型和XmlInclude属性。例如:
public class Book
{
public string Title;
public string Author;
}
public class MyBookType : Book { }
[XmlInclude(typeof(MyBookType))]
[XmlRoot("Books")]
public class Books : List<Book> { }
public void Run()
{
var b = new Books();
b.Add(new MyBookType
{
Title = "The Art of War",
Author = "Sun Tzu"
});
b.Add(new MyBookType
{
Title = "Great Expectations",
Author = "Charles Dickens"
});
var s = new XmlSerializer(typeof(Books));
s.Serialize(Console.Out, b);
}
Running this produces this output:
运行此命令将产生以下输出:
<?xml version="1.0" encoding="IBM437"?>
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Book xsi:type="MyBookType">
<Title>The Art of War</Title>
<Author>Sun Tzu</Author>
</Book>
<Book xsi:type="MyBookType">
<Title>Great Expectations</Title>
<Author>Charles Dickens</Author>
</Book>
</Books>
Since you're using a SOAP request, I'm assuming ASMX, which means the serialization happens implicitly. You will need to apply [XmlInclude]
to whatever holds the collection of books. This could be a parameter in a webmethod, for example.
因为您正在使用SOAP请求,所以我假设是ASMX,这意味着序列化是隐式的。您将需要将[XmlInclude]应用到包含图书集合的任何地方。例如,这可以是webmethod中的一个参数。
You can automatically generate the appropriate XmlInclude attribute, starting from XSD and WSDL, if you define the types in XSD, with the inheritance relationship I illustrated in C# code.
如果您在XSD中定义了类型,您可以从XSD和WSDL开始自动生成适当的XmlInclude属性,继承关系如c#代码中所示。
In WSDL, the request message might take a Books
type, which is a collection of Book
. Separately, define a MyBookType
which derives from Book but does not extend it.
在WSDL中,请求消息可能采用图书类型,这是图书的集合。另外,定义一个MyBookType,它派生自Book,但不扩展它。