How do I control the <?xml ?> part of xml serialization with .NET?

后端 未结 3 2005
一个人的身影
一个人的身影 2020-12-16 16:11

I am using this method to serialize my object:

public static string XmlSerialize(object o)
{
    var stringWriter = new StringWriter();
    var xmlSerializer         


        
相关标签:
3条回答
  • 2020-12-16 16:41

    FWIW, I got the encoding to work by using an XmlWriter with XMLWriterSettings. Here is a sample:

    ...  
    // My object type was from a class generated by xsd.  
    XmlSerializer xms = new XmlSerializer(typeof(SomeType));  
    SomeType objSt;
    using(FileStream fs = new FileStream("C:\SomeFile.xml", FileMode.Open, FileAccess.Read))
    {    
      using(XmlReader xr = XmlReader.Create(fs))  // Supposed to preserve encoding.  
      {  
        objSt = (SomeType)xms.Deserialize(xr);  
      }    
    }  
    
    ...  
    ...  // Do some stuff, change some attribute values.  
    ...  
    
    XmlWriterSettings xsw= new XmlWriterSettings();  
    xsw.Indent= true;  
    xsw.NewLineOnAttributes= true;  
    xsw.Encoding = Encoding.GetEncoding(1252);  
    using(XmlWriter xwXsw = XmlWriter.Create("C:\SomeFile_Changed.xml",xsw))
    {    
      xms.Serialize(xwXsw, objSt);  
    }  
    ...  
    ...  // Finish up and get out.
    ...  
    

    For some reason, I was able to get it all to work once just using the XmlSerializer object and serializing with a TextWriter, since according to MS help for XmlSerializer.Deserialize(XmlReader) "The XmlReader automatically detects and uses the encoding specified by the XML document." Then I started playing with the XmlWriterSettings and broke something....

    0 讨论(0)
  • 2020-12-16 16:45

    Try this:

    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();
        }
    }
    

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


    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.

    0 讨论(0)
  • 2020-12-16 16:45

    You can use an XmlTextWriter instead of a StringWriter. Here is an extract from some of my code with your encoding set.

    XmlTextWriter textWriter = new XmlTextWriter(stream, Encoding.GetEncoding(1252));
    textWriter.Namespaces = false;
    
    0 讨论(0)
提交回复
热议问题