如何在XML序列化中省略前缀

时间:2019-04-15 16:41:47

标签: c# xml-serialization xml-namespaces

我想知道是否可以使用System.Xml.Serialization从序列化对象中省略XML标记内的名称空间前缀。标记了源对象类以获取正确的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; }
    }

0 个答案:

没有答案