我如何控制在<?XML> XML序列化与.NET的一部分吗?序列、化与、XML、lt

2023-09-03 00:07:47 作者:人丑多作怪

我使用这种方法序列化我的对象:

I am using this method to serialize my object:

public static string XmlSerialize(object o)
{
    var stringWriter = new StringWriter();
    var xmlSerializer = new XmlSerializer(o.GetType());
    xmlSerializer.Serialize(stringWriter, o);
    string xml = stringWriter.ToString();
    stringWriter.Close();
    return xml;
}

这使得XML的开始是这样的:

It makes XML that starts like this:

<?xml version="1.0" encoding="utf-16"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

但我希望它看起来是这样的:

But I want it to look like this:

<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

那么,我该如何更改编码到Windows 1252,并设置独立的= YES? 此外,如何我得到排除的xmlns值的对象?

So, how do I change the encoding to Windows-1252 and set standalone = yes? Additionally, how to I get the object to exclude the xmlns value?

我见过一对夫妇类似的问题,如这一个,但我希望这可能是简单的对我来说,也许是地方设置一些属性?

I've seen a couple similar questions, like this one, but I was hoping it might be simpler for me, maybe by setting some attributes somewhere?

更新2:细算约翰的回答和评论,并想了解更多这方面,我决定只让第二个方法。我不认为建立这个古怪的自定义XML只是在一个场合第三方应该叫摆在首位的东西作为一般的XMLSERIALIZE。

Update 2: After looking at John's answer and comments, and thinking about this more, I decided to just make a second method. I don't think that creating this wacky custom xml just for a 3rd party on one occasion should be called something as generic as "XmlSerialize" in the first place.

所以,我创建了以XML文档为第二个方法和第一,消除了一个命名空间的元素是这样的:

So, I created a second method that takes an XML document and first, removes the one namespace element like this:

xElement.Attributes().Where(a => a.IsNamespaceDeclaration && a.Value == "http://www.w3.org/2001/XMLSchema").Remove();

然后,它把它写到XML与约翰的code。最后,它返回的XML,继从这个输出:

then, it it writes it to XML with John's code. Finally it returns that xml, following the output from this:

new XDeclaration("1.0", "Windows-1252", "yes").ToString()

这是丑陋的,但它让我正是我需要这个第三方来了解我的XML。

And that's ugly, but it gets me exactly what I need for this 3rd party to understand my XML.

推荐答案

试试这个:

public static string XmlSerialize(object o)
{
    using (var stringWriter = new StringWriter())
    {
        var settings = new XmlWriterSettings
                           {
                               Encoding = Encoding.GetEncoding(1252),
                               OmitXmlDeclaration = true
                           };
        using (var writer = XmlWriter.Create(stringWriter, settings))
        {
            var xmlSerializer = new XmlSerializer(o.GetType());
            xmlSerializer.Serialize(writer, o);
        }
        return stringWriter.ToString();
    }
}

这将无法摆脱为xsd:命名空间,不过呢,你为什么要

This won't get rid of the xsd: namespace, but then, why do you want to?

更新:看来,无论你使用一个的StringWriter ,你得到UTF-16,即使你使用在它的上面有编码集的XmlWriter 。下一步将是写出一个的MemoryStream 。但是,这引起了你为什么要返回一个字符串的问题。举例来说,如果你要只是转身输出字符串流,那么我们就应该直接输出到该流。同样适用于的TextWriter

Update: It seems that whenever you use a StringWriter, you get UTF-16, even if you use an XmlWriter on top of it with encoding set. Next step would be to write out to a MemoryStream. But that raises the question of why you want to return a string. For instance, if you're going to just turn around and output the string to a stream, then we should output directly to this stream. Same for a TextWriter.