c# - 如何在 XML 序列化中省略前缀
问题描述
我想知道是否可以使用 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; }
}
解决方案
推荐阅读
- python-3.x - 为什么在一些代码之后它会自动更改数据类型
- angular - 与 Laravel 一起使用时,如何在 Angular 等 Vuejs 中创建界面?
- c# - 如何在 LINQ to SQL 的第二次连接中使用第一次连接的结果
- java - 调用接口上不存在的实现方法
- typo3 - 使用 TYPO3 9.5.8 中的打字稿为 tx_news 生成讲话 url
- sql - Linux机器上R MySQL中的字符编码
- sql - 如何在 JPA 中根据 group by 过滤条件?
- android - 从 android 中的 Mediastore 查询中获取数据时,光标返回 null
- c# - 从 DSL 中解析变量名和数组索引
- azure - 是否可以通过 Azure API 创建具有开放端口的 Azure VM?