如何使 XmlSerialiser 不以 开头?

发布于 2024-08-03 20:02:45 字数 154 浏览 12 评论 0原文

我正在使用的文件格式 (OFX) 是类似 XML 的,并且在类似 XML 的位开始之前包含一堆纯文本内容。但它不喜欢在纯文本和 XML 部分之间存在,所以我想知道是否有办法让 XmlSerialiser 忽略它。我知道我可以浏览该文件并删除该行,但一开始就不写它会更简单、更干净!有什么想法吗?

The file format I'm working with (OFX) is XML-like and contains a bunch of plain-text stuff before the XML-like bit begins. It doesn't like having between the plain-text and XML parts though, so I'm wondering if there's a way to get XmlSerialiser to ignore that. I know I could go through the file and wipe out that line but it would be simpler and cleaner to not write it in the first place! Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-08-10 20:02:45

您必须操作在调用 序列化方法。其 Settings 属性具有 OmitXmlDeclaration 属性,您需要将其设置为 true。您还需要设置 ConformanceLevel 属性,否则 XmlWriter 将忽略 OmitXmlDeclaration 属性。

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlWriter writer = XmlWriter.Create(/*whatever stream you need*/,settings);
serializer.Serialize(writer,objectToSerialize);
writer.close();

You'll have to manipulate the XML writer object you use when calling the Serialize method. Its Settings property has a OmitXmlDeclaration property, which you'll want to set to true. You'll also need to set the ConformanceLevel property, otherwise the XmlWriter will ignore the OmitXmlDeclaration property.

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlWriter writer = XmlWriter.Create(/*whatever stream you need*/,settings);
serializer.Serialize(writer,objectToSerialize);
writer.close();
成熟稳重的好男人 2024-08-10 20:02:45

不太难,您只需序列化到显式声明的 XmlWriter 并在序列化之前设置该编写器的选项即可。

public static string SerializeExplicit(SomeObject obj)
{    
    XmlWriterSettings settings;
    settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;

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


    XmlSerializer serializer;
    serializer = new XmlSerializer(typeof(SomeObject));

    //Or, you can pass a stream in to this function and serialize to it.
    // or a file, or whatever - this just returns the string for demo purposes.
    StringBuilder sb = new StringBuilder();
    using(var xwriter = XmlWriter.Create(sb, settings))
    {

        serializer.Serialize(xwriter, obj, ns);
        return sb.ToString();
    }
}

Not too tough, you just have to serialize to an explicitly declared XmlWriter and set the options on that writer before you serialize.

public static string SerializeExplicit(SomeObject obj)
{    
    XmlWriterSettings settings;
    settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;

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


    XmlSerializer serializer;
    serializer = new XmlSerializer(typeof(SomeObject));

    //Or, you can pass a stream in to this function and serialize to it.
    // or a file, or whatever - this just returns the string for demo purposes.
    StringBuilder sb = new StringBuilder();
    using(var xwriter = XmlWriter.Create(sb, settings))
    {

        serializer.Serialize(xwriter, obj, ns);
        return sb.ToString();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文