Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何告诉xmlwriter忽略C#中的空节点?_C#_Xml - Fatal编程技术网

如何告诉xmlwriter忽略C#中的空节点?

如何告诉xmlwriter忽略C#中的空节点?,c#,xml,C#,Xml,我正在将以下实体序列化为XML,以发送到我们的Google搜索设备: [Serializable] [XmlType("record")] public class GSADocumentRecord { public enum RecordActions { Add, Delete } [XmlAttribute(AttributeName = "url")] public string URL { get; set;

我正在将以下实体序列化为XML,以发送到我们的Google搜索设备:

[Serializable]
[XmlType("record")]
public class GSADocumentRecord
{
    public enum RecordActions
    {
        Add,
        Delete
    }

    [XmlAttribute(AttributeName = "url")]
    public string URL { get; set; }

    [XmlAttribute(AttributeName = "mimetype")]
    public string MimeType { get; set; }

    [XmlAttribute(AttributeName = "last-modified")]
    public string LastModified { get; set; }

    [XmlAttribute(AttributeName = "action")]
    public string Action { get; set; }

    [XmlArray(ElementName = "metadata", Order = 0)]
    public List<GSADocumentRecordMeta> MetaData { get; set; }

    [XmlElement(ElementName = "content", Order = 1, Type = typeof(CDATA))]
    public CDATA Content { get; set; }
}

如果列表为空,如何告诉
XmlWriter
忽略此元素

拥有一个自动关闭标签显然是合法的,这听起来像是他们这边的解析器造成了问题。您可以先将XML写入字符串,然后执行
。替换(“,”)

当有(例如)两个
元数据
条目时,预期的XML是什么?是的,我想我必须这样做,很好奇是否有更好的解决方案这并不理想,但有时您必须使用有效的解决方案。:)
        var ms = new System.IO.MemoryStream();
        XmlSerializer xml = new XmlSerializer(this.GetType());

        StreamWriter sw = new StreamWriter(ms);
        XmlWriter xw = new XmlTextWriter(sw);

        xw.WriteStartDocument();
        xw.WriteDocType("gsafeed", "-//Google//DTD GSA Feeds//EN", null, null);

        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        xml.Serialize(xw, this, ns);

        ms.Position = 0;