Как опустить префикс в XML-сериализации - PullRequest
0 голосов
/ 15 апреля 2019

Я хотел бы знать, возможно ли исключить префикс пространства имен в теге XML из сериализованного объекта, используя System.Xml.Serialization.Класс исходного объекта помечен для получения правильного XML-документа.

Вот XML-код, который я получаю:

<?xml version="1.0" encoding="utf-8"?>
<ns:ContainerAddRequest xmlns:ns="enterprise.com/common/messages" xmlns:commonTypes="enterprise.com/common/types"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   RequestID="0324e614-085a-49c1-aeb9-81604314fbbe"
   Timestamp="2019-04-15T15:29:14.738555+02:00" >
  <commonTypes:ContainerInformation xsi:type="commonTypes:SingleContainerInfo">
    <Container>
      <GUID GUID="a591e007-07cc-4aba-a318-c3b0c2d53193" />
      <Lenght>6068</Lenght>
      <Height>2348</Height>
    </Container>
    <commonTypes:Position xsi:type="commonTypes:SlotPosition">
      <commonTypes:Stack xsi:type="commonTypes:FixedStack">
        <AreaID>A10</AreaID>
      </commonTypes:Stack>
      <Tier>A</Tier>
      <DoorOrientation>South</DoorOrientation>
    </commonTypes:Position>
  </commonTypes:ContainerInformation>
</ns:ContainerAddRequest>

И вот этот XML-код я бы хотел получить:

<?xml version="1.0" encoding="utf-8"?>
<ns:ContainerAddRequest xmlns:ns="enterprise.com/common/messages"
    xmlns:commonTypes="enterprise.com/common/types"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    RequestID="0324e614-085a-49c1-aeb9-81604314fbbe"
    Timestamp="2019-04-15T15:29:14.738555+02:00" >
  <ContainerInformation xsi:type="commonTypes:SingleContainerInfo">
    <Container>
      <GUID GUID="a591e007-07cc-4aba-a318-c3b0c2d53193" />
      <Lenght>6068</Lenght>
      <Height>2348</Height>
    </Container>
    <Position xsi:type="commonTypes:SlotPosition">
      <Stack xsi:type="commonTypes:FixedStack">
        <AreaID>A10</AreaID>
      </Stack>
      <Tier>A</Tier>
      <DoorOrientation>South</DoorOrientation>
    </Position>
  </ContainerInformation>
</ns:ContainerAddRequest>

Как видите, узлы ContainerInformation, Position и Stack не имеют префикса "CommonTypes:"

Заранее спасибо.

По поводу определения классов здесь ContainerAddRequest

    [XmlRoot(ElementName = "ContainerAddRequest", Namespace = "enterprise.com/common/messages")]
    public class ContainerAddRequest : Request
    {
        [XmlIgnore]
        public MessageHeader Header { get; set; }

        [XmlElement(ElementName = "ContainerInformation", Namespace = "enterprise.com/common/types")]
        public ContainerInfoBase ContainerInformation { get; set; }

        [XmlElement(ElementName = "CHEID", Namespace = "")]
        public string CHEID { get; set; }
    }

и ContainerInfoBase является абстрактным классом,

    [XmlInclude (typeof(SingleContainerInfo))]
    [XmlInclude (typeof(TwinCompositionInfo))]
    [XmlInclude (typeof(QuadCompositionInfo))]
    [XmlInclude (typeof(TandemCompositionInfo))]
    public abstract class ContainerInfoBase { }

и, наконец, здесь SingleContainerInfo класс,

    [XmlRoot(ElementName = "SingleContainerInfo")]
    public class SingleContainerInfo : ContainerInfoBase
    {
        [XmlElement(ElementName = "Container", Namespace = "")]
        public Container Container { get; set; }

        [XmlElement(ElementName = "Position")]
        public PositionBase Position { get; set; }
    }
...