当没有数据时,防止XmlSerializer中的自闭标签
当我序列化值时:如果数据中没有值,那么它就像下面的格式一样。
Acknowledged by PPS
但我想要的xml数据格式如下:
Acknowledged by PPS
代码为此我写了:
[Serializable] public class Notes { [XmlElement("Type")] public string typeName { get; set; } [XmlElement("Data")] public string dataValue { get; set; } }
如果数据没有分配任何值,我无法弄清楚如何以下面的格式实现数据。
Acknowledged by PPS
您可以通过创建自己的XmlTextWriter来传递进入序列化过程。
public class MyXmlTextWriter : XmlTextWriter { public MyXmlTextWriter(Stream stream) : base(stream, Encoding.UTF8) { } public override void WriteEndElement() { base.WriteFullEndElement(); } }
您可以使用以下方法测试结果:
class Program { static void Main(string[] args) { using (var stream = new MemoryStream()) { var serializer = new XmlSerializer(typeof(Notes)); var writer = new MyXmlTextWriter(stream); serializer.Serialize(writer, new Notes() { typeName = "Acknowledged by PPS", dataValue="" }); var result = Encoding.UTF8.GetString(stream.ToArray()); Console.WriteLine(result); } Console.ReadKey(); }
IMO无法使用Serialization
生成所需的XML。 但是,您可以使用LINQ to XML
生成所需的架构,如下所示 –
XDocument xDocument = new XDocument(); XElement rootNode = new XElement(typeof(Notes).Name); foreach (var property in typeof(Notes).GetProperties()) { if (property.GetValue(a, null) == null) { property.SetValue(a, string.Empty, null); } XElement childNode = new XElement(property.Name, property.GetValue(a, null)); rootNode.Add(childNode); } xDocument.Add(rootNode); XmlWriterSettings xws = new XmlWriterSettings() { Indent=true }; using (XmlWriter writer = XmlWriter.Create("D:\\Sample.xml", xws)) { xDocument.Save(writer); }
in case your value is null, you should set it to empty string
则主要捕获in case your value is null, you should set it to empty string
。 它将force the closing tag to be generated
。 如果value为null,则不会创建结束标记。
Kludge时间 – 请参阅生成在HTML中有效的System.Xml.XmlDocument.OuterXml()输出
基本上,在生成XML doc后,遍历每个节点,如果没有子节点,则添加一个空文本节点
// Call with addSpaceToEmptyNodes(xmlDoc.FirstChild); private void addSpaceToEmptyNodes(XmlNode node) { if (node.HasChildNodes) { foreach (XmlNode child in node.ChildNodes) addSpaceToEmptyNodes(child); } else node.AppendChild(node.OwnerDocument.CreateTextNode("")) }
(是的,我知道您不应该这样做 – 但如果您将XML发送到其他一些您无法轻易解决的系统,那么必须务必做事)
您可以添加虚拟字段以防止自闭合元素。
[XmlText] public string datavalue= " ";
或者如果你想要你的类的代码,那么你的类应该是这样的。
public class Notes { [XmlElement("Type")] public string typeName { get; set; } [XmlElement("Data")] private string _dataValue; public string dataValue { get { if(string.IsNullOrEmpty(_dataValue)) return " "; else return _dataValue; } set { _dataValue = value; } } }