开发者

XmlSerializerFormat not nested XML

开发者 https://www.devze.com 2022-12-18 16:06 出处:网络
For my project I am using XmlSerializerFormat for serialization. I have a class for example: [SoapInclude(typeof(Thing))]

For my project I am using XmlSerializerFormat for serialization. I have a class for example:

[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 which is generated from this class has the following format:

<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>
&l开发者_运维问答t;/wrapper>

However XML which I need should be in the following format:

<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>

I would really appreciate if someone has a solution for this problem.


xml and code was not visible.

This is a sample class:

[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;

 }

generated XML looks like this

 <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 which I need should be in the following format:

  <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号