【问题标题】:how to create an xml using xml writer without declaration element如何使用没有声明元素的 xml 编写器创建 xml
【发布时间】:2011-07-26 16:42:24
【问题描述】:

我正在使用XmlWriter.Create() 获取写入器实例然后写入XML,但结果有<?xml version="1.0" encoding="utf-16" ?>,我如何告诉我的xml 写入器不生成它?

【问题讨论】:

    标签: c# .net xml xmlwriter


    【解决方案1】:

    使用XmlWriterSettings.OmitXmlDeclaration

    不要忘记将XmlWriterSettings.ConformanceLevel 设置为ConformanceLevel.Fragment

    【讨论】:

    • 来自您链接到的文档:The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true
    • 不会大部分时间都设置为Document吧?
    • @Cameron,奇怪的问题...ConformanceLevel.Document 是默认设置,但您可以设置ConformanceLevel.Fragment...
    • 啊,是的,我知道知道 :-) 您可能想将其添加到您的答案中。如果您想要 Document 一致性级别(例如,确保单个根节点),但没有 XML 声明,该怎么办?
    • @Cameron,我想你自己回答了你的问题 :-)
    【解决方案2】:

    您可以继承 XmlTextWriter 并覆盖 WriteStartDocument() 方法以不做任何事情:

    public class XmlFragmentWriter : XmlTextWriter
    {
        // Add whichever constructor(s) you need, e.g.:
        public XmlFragmentWriter(Stream stream, Encoding encoding) : base(stream, encoding)
        {
        }
    
        public override void WriteStartDocument()
        {
           // Do nothing (omit the declaration)
        }
    }
    

    用法:

    var stream = new MemoryStream();
    var writer = new XmlFragmentWriter(stream, Encoding.UTF8);
    // Use the writer ...
    

    参考:来自 Scott Hanselman 的 blog post

    【讨论】:

    • 谢谢,有更好的方法吗?我不想仅仅为了删除声明而进行子类化。
    • @ninithepug:据我所知,抱歉。不过,如果您经常使用它,您可以将它封装在某个地方的静态方法中。这应该有助于保持清洁
    • @ninithepug:似乎 Kirill 的答案(不需要新类)可能会更好(当然,除非你想要 Document 一致性级别)。
    【解决方案3】:

    您可以将XmlWriter.Create() 用于:

    new XmlWriterSettings { 
        OmitXmlDeclaration = true, 
        ConformanceLevel = ConformanceLevel.Fragment 
    }
    
        public static string FormatXml(string xml)
        {
            if (string.IsNullOrEmpty(xml))
                return string.Empty;
    
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);
                using (MemoryStream memoryStream = new MemoryStream())
                using (XmlWriter writer = XmlWriter.Create(
                    memoryStream, 
                    new XmlWriterSettings { 
                        Encoding = Encoding.Unicode, 
                        OmitXmlDeclaration = true, 
                        ConformanceLevel = ConformanceLevel.Fragment, 
                        Indent = true, 
                        NewLineOnAttributes = false }))
                {
                    document.WriteContentTo(writer);
                    writer.Flush();
                    memoryStream.Flush();
                    memoryStream.Position = 0;
                    using (StreamReader streamReader = new StreamReader(memoryStream))
                    {
                        return streamReader.ReadToEnd();
                    }
                }
            }
            catch (XmlException ex)
            {
                return "Unformatted Xml version." + Environment.NewLine + ex.Message;
            }
            catch (Exception ex)
            {
                return "Unformatted Xml version." + Environment.NewLine + ex.Message;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 2016-04-24
      相关资源
      最近更新 更多