对于我的项目,我使用XmlSerializerFormat进行序列化。我有一个班级,例如:
[SoapInclude(typeof(Thing))]
[SoapType("Thing", "TheNs", IncludeInSchema = true)]
public class Thing
{
[SoapElement(IsNullable = true, DataType = "string")]
public string ThingName = "aap";
}
[SoapType("Transportation", "TheNs", IncludeInSchema = true)]
[SoapInclude(typeof(Transportation))]
public class Transportation
{
// The SoapElementAttribute specifies that the
// generated XML element name will be "Wheels"
// instead of "Vehicle".
[SoapElement("Wheels")]
public string Vehicle;
[SoapElement(DataType = "dateTime")]
public DateTime CreationDate;
[SoapElement(IsNullable = true)]
public Thing Thing;
}
从此类生成的XML具有以下格式:
<wrapper>
<q1:Transportation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" xmlns:q1="TheNs">
<Truck xsi:type="xsd:string">MyCar</Truck>
<CreationDate xsi:type="xsd:dateTime">2010-01-05T16:03:01.9436034+01:00</CreationDate>
<Thing href="#id2" />
</q1:Transportation>
<q2:Thing id="id2" d2p1:type="q2:Thing" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" xmlns:q2="TheNs">
<ThingName xmlns:q3="http://www.w3.org/2001/XMLSchema" d2p1:type="q3:string">thing</ThingName>
</q2:Thing>
</wrapper>
但是我需要的XML应该采用以下格式:
<wrapper>
<Transportation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Vehicle>MyCar</Vehicle>
<CreationDate>2010-01-05T16:03:01.7562378+01:00</CreationDate>
<Thing>
<ThingName>thing</ThingName>
</Thing>
</Transportation>
</wrapper>
如果有人能解决这个问题我真的很感激。
xml和代码不可见。
这是一个示例类:
[SoapInclude(typeof(Thing))]
[SoapType("Thing", "TheNs", IncludeInSchema = true)]
public class Thing
{
[SoapElement(IsNullable = true, DataType = "string")]
public string ThingName = "thing";
}
[SoapType("Transportation", "TheNs", IncludeInSchema = true)]
[SoapInclude(typeof(Transportation))]
public class Transportation
{
// The SoapElementAttribute specifies that the
// generated XML element name will be "Wheels"
// instead of "Vehicle".
[SoapElement("Wheels")]
public string Vehicle;
[SoapElement(DataType = "dateTime")]
public DateTime CreationDate;
[SoapElement(IsNullable = true)]
public Thing Thing;
}
生成的XML看起来像这样
<wrapper>
<q1:Transportation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" xmlns:q1="TheNs">
<Truck xsi:type="xsd:string">MyCar</Truck>
<CreationDate xsi:type="xsd:dateTime">2010-01-05T16:03:01.9436034+01:00</CreationDate>
<Thing href="#id2" />
</q1:Transportation>
<q2:Thing id="id2" d2p1:type="q2:Thing" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" xmlns:q2="TheNs">
<ThingName xmlns:q3="http://www.w3.org/2001/XMLSchema" d2p1:type="q3:string">thing</ThingName>
</q2:Thing>
</wrapper>
我需要的XML应该采用以下格式:
<wrapper>
<Transportation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Vehicle>MyCar</Vehicle>
<CreationDate>2010-01-05T16:03:01.7562378+01:00</CreationDate>
<Thing>
<ThingName>thing</ThingName>
</Thing>
</Transportation>
</wrapper>