Omitting XML processing instruction when serializing an object

只谈情不闲聊 提交于 2019-12-18 00:32:53

问题


I'm serializing an object in a C# VS2003 / .Net 1.1 application. I need it serialized without the processing instruction, however. The XmlSerializer class puts out something like this:

<?xml version="1.0" encoding="utf-16" ?> 
<MyObject>
    <Property1>Data</Property1>
    <Property2>More Data</Property2>
</MyObject>

Is there any way to get something like the following, without processing the resulting text to remove the tag?

<MyObject>
    <Property1>Data</Property1>
    <Property2>More Data</Property2>
</MyObject>

For those that are curious, my code looks like this...

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();

using ( TextWriter stringWriter = new StringWriter(builder) )
{
    serializer.Serialize(stringWriter, comments);
    return builder.ToString();
}

回答1:


The following link will take you to a post where someone has a method of supressing the processing instruction by using an XmlWriter and getting into an 'Element' state rather than a 'Start' state. This causes the processing instruction to not be written.

Suppress Processing Instruction

If you pass an XmlWriter to the serializer, it will only emit a processing instruction if the XmlWriter's state is 'Start' (i.e., has not had anything written to it yet).

// Assume we have a type named 'MyType' and a variable of this type named 
'myObject' 
System.Text.StringBuilder output = new System.Text.StringBuilder(); 
System.IO.StringWriter internalWriter = new System.IO.StringWriter(output); 
System.Xml.XmlWriter writer = new System.Xml.XmlTextWriter(internalWriter); 
System.Xml.Serialization.XmlSerializer serializer = new 
System.Xml.Serialization.XmlSerializer(typeof(MyType)); 


writer.WriteStartElement("MyContainingElement"); 
serializer.Serialize(writer, myObject); 
writer.WriteEndElement(); 

In this case, the writer will be in a state of 'Element' (inside an element) so no processing instruction will be written. One you finish writing the XML, you can extract the text from the underlying stream and process it to your heart's content.




回答2:


I made a small correction

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using ( XmlWriter stringWriter = XmlWriter.Create(builder, settings) )
{   
   serializer.Serialize(stringWriter, comments);  
  return builder.ToString();
}



回答3:


In 2.0, you would use XmLWriterSettings.OmitXmlDeclaration, and serialize to an XmlWriter - however I don't think this exists in 1.1; so not entirely useful - but just one more "consider upgrading" thing... and yes, I realise it isn't always possible.




回答4:


What about omitting namespaces ?

instead of using

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

ex:

<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">



回答5:


If by "processing instruction" you mean the xml declaration, then you can avoid this by setting the OmitXmlDeclaration property of XmlWriterSettings. You'll need to serialize using an XmlWriter, to accomplish this.

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

using ( XmlWriter stringWriter = new StringWriter(builder, settings) )
{
    serializer.Serialize(stringWriter, comments);
    return builder.ToString();
}

But ah, this doesn't answer your question for 1.1. Well, for reference to others.




回答6:


This works in .NET 1.1. (But you should still consider upgrading)

    XmlSerializer s1= new XmlSerializer(typeof(MyClass)); 
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add( "", "" );


    MyClass c= new MyClass();
    c.PropertyFromDerivedClass= "Hallo";

    sw = new System.IO.StringWriter();
    s1.Serialize(new XTWND(sw), c, ns);
    ....

   /// XmlTextWriterFormattedNoDeclaration
   /// helper class : eliminates the XML Documentation at the
   /// start of a XML doc. 
   /// XTWFND = XmlTextWriterFormattedNoDeclaration
   public class XTWFND : System.Xml.XmlTextWriter
   {
     public XTWFND(System.IO.TextWriter w) : base(w) { Formatting = System.Xml.Formatting.Indented; }
     public override void WriteStartDocument() { }
   }


来源:https://stackoverflow.com/questions/164585/omitting-xml-processing-instruction-when-serializing-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!