如何在没有数据时阻止XmlSerializer中的自闭关标签

编程入门 行业动态 更新时间:2024-10-11 13:19:18
本文介绍了如何在没有数据时阻止XmlSerializer中的自闭关标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

public string XmlSerializer(Object item) { StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Indent = true; using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings)) { XmlSerializer xmlSerializer = new XmlSerializer(item.GetType()); XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); nameSpaces.Add("", ""); xmlSerializer.Serialize(xmlWriter, item, nameSpaces); } return builder.ToString(); }

O / P: -

O/P :-

<get_country_details_response> <country_id>10.0000</country_id> <country_name>10.0000</country_name> <currency_name /> <!-- Need to Avoid This Self Closing Tag --> </get_country_details_response>

推荐答案

一个选项是使用正则表达式对输出进行一些后处理: 首先定义以下正则表达式以找到空标签: One option is to perform some post-processing of the output using regular expressions: First define the following Regex to find the empty tags: static Regex emptyElementRegex = new Regex(@"<(\w+)\s*/>");

然后处理您的输出以替换所有匹配的事件:

Then process your output to replace all matching occurrences:

var result = builder.ToString(); result = emptyElementRegex.Replace(result, @"<

1>); 1>");

这应该转换所有没有属性的自动关闭标签。

This should transform ALL self-closing tags without attributes.

更多推荐

如何在没有数据时阻止XmlSerializer中的自闭关标签

本文发布于:2023-11-06 03:27:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   数据   如何在   XmlSerializer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!