从对象类生成XML时ns2:前缀缺失的问题排查
问题排查:XML根节点缺失ns2前缀的原因与修复
问题概述
期望生成的XML根节点为<ns2:LoadStatusData>,并带有指定命名空间声明,但实际生成的XML根节点缺失ns2:前缀。
目标XML
<?xml version="1.0"?> <ns2:LoadStatusData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://schemas.3gtms.com/tms/v1/tns"> <BatchInfo> <BatchNum>20230420175634</BatchNum> <BatchDateTime>2023-04-20T17:56:34.5647378+05:30</BatchDateTime> <SentBy>NIN-Integration</SentBy> <PageNum>0</PageNum> <PageCnt>0</PageCnt> <NullPrimaryEmptyElements>false</NullPrimaryEmptyElements> </BatchInfo> </ns2:LoadStatusData>
当前生成的XML(存在问题)
<LoadStatusData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://schemas.3gtms.com/tms/v1/tns"> <BatchInfo> <BatchNum>20230420181158</BatchNum> <BatchDateTime>2023-04-20T18:11:58.7330956+05:30</BatchDateTime> <SentBy>NIN-Integration</SentBy> <PageNum>0</PageNum> <PageCnt>0</PageCnt> <NullPrimaryEmptyElements>false</NullPrimaryEmptyElements> </BatchInfo> </LoadStatusData>
错误点分析
- 根节点未绑定目标命名空间与前缀:
LoadStatusData类的[XmlRoot]属性仅指定了元素名,未关联http://schemas.3gtms.com/tms/v1/tns命名空间及ns2前缀,导致根节点不属于该命名空间,不会自动添加前缀。 - 错误定义命名空间声明属性:代码中用
[XmlAttribute(AttributeName="ns2")]手动定义xmlns:ns2,但XML命名空间声明是由序列化器自动处理的,无需手动添加此类属性。 - BatchNum类型不匹配:目标XML中
BatchNum是字符串类型的批次号,代码中使用double类型,可能导致序列化时出现格式异常(如科学计数法)。
修正后的代码
[XmlRoot(ElementName = "BatchInfo")] public class BatchInfo { // 修正为string类型,匹配目标XML的格式 [XmlElement(ElementName = "BatchNum")] public string BatchNum { get; set; } [XmlElement(ElementName = "BatchDateTime")] public DateTime BatchDateTime { get; set; } [XmlElement(ElementName = "SentBy")] public string SentBy { get; set; } [XmlElement(ElementName = "PageNum")] public int PageNum { get; set; } [XmlElement(ElementName = "PageCnt")] public int PageCnt { get; set; } [XmlElement(ElementName = "NullPrimaryEmptyElements")] public bool NullPrimaryEmptyElements { get; set; } } // 指定根节点的命名空间、元素名和前缀 [XmlRoot(ElementName = "LoadStatusData", Namespace = "http://schemas.3gtms.com/tms/v1/tns", NamespacePrefix = "ns2")] public class LoadStatusData { [XmlElement(ElementName = "BatchInfo")] public BatchInfo BatchInfo { get; set; } // 声明xsd和xsi命名空间,XmlSerializer会自动生成xmlns:xsd/xmlns:xsi属性 [XmlNamespaceDeclarations] public XmlSerializerNamespaces XmlNamespaces { get; set; } = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("xsd", "http://www.w3.org/2001/XMLSchema"), new XmlQualifiedName("xsi", "http://www.w3.org/2001/XMLSchema-instance") }); }
序列化代码示例
var data = new LoadStatusData { BatchInfo = new BatchInfo { BatchNum = "20230420175634", BatchDateTime = DateTime.Parse("2023-04-20T17:56:34.5647378+05:30"), SentBy = "NIN-Integration", PageNum = 0, PageCnt = 0, NullPrimaryEmptyElements = false } }; var serializer = new XmlSerializer(typeof(LoadStatusData)); using (var writer = new StringWriter()) { serializer.Serialize(writer, data, data.XmlNamespaces); Console.WriteLine(writer.ToString()); }
修正后生成的XML
<?xml version="1.0" encoding="utf-16"?> <ns2:LoadStatusData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://schemas.3gtms.com/tms/v1/tns"> <BatchInfo> <BatchNum>20230420175634</BatchNum> <BatchDateTime>2023-04-20T17:56:34.5647378+05:30</BatchDateTime> <SentBy>NIN-Integration</SentBy> <PageNum>0</PageNum> <PageCnt>0</PageCnt> <NullPrimaryEmptyElements>false</NullPrimaryEmptyElements> </BatchInfo> </ns2:LoadStatusData>
内容的提问来源于stack exchange,提问作者A.Goutam
相关产品推荐
相关产品推荐

