我有许多XSD必须用作Web服务请求。当我将请求序列化为字符串(或文件)时,根元素始终作为xml元素重复。为什么是这样?我该如何防止这种情况?
我使用LinqToXsd(nuget包)V2.0.2。与VS2013。
XSD:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://blah.com" targetNamespace="http://blah.com">
<xsd:complexType name="SomeValues">
<xsd:sequence>
<xsd:element name="Value1" type="xsd:int" minOccurs="0"/>
<xsd:element name="Value2" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
生成的XML(如您所见,根&#39; SomeValues&#39;重复):
<?xml version="1.0" encoding="utf-16"?>
<SomeValues>
<SomeValues xmlns="http://blah.com">
<Value1 xmlns="">1</Value1>
<Value2 xmlns="">2</Value2>
</SomeValues>
</SomeValues>
这是序列化片段:
private static string SerializeTest()
{
var content = new SomeValues
{
Value1 = 1,
Value2 = 2
};
XmlSerializer serializer = new XmlSerializer(content.GetType());
using (var stringWriter = new StringWriter())
{
serializer.Serialize(stringWriter, content);
return stringWriter.ToString();
}
}