序列化对象时省略 XML 处理指令

发布于 2024-07-05 21:29:25 字数 808 浏览 5 评论 0原文

我正在 C# VS2003 / .Net 1.1 应用程序中序列化一个对象。 但是,我需要在没有处理指令的情况下对其进行序列化。 XmlSerializer 类输出如下内容:

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

有没有办法获得类似以下内容,而不处理结果文本以删除标记?

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

对于那些好奇的人,我的代码如下所示......

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

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

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

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

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

发布评论

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

评论(6

夏末染殇 2024-07-12 21:29:25

在 2.0 中,您可以使用 XmLWriterSettings.OmitXmlDeclaration ,并序列化为 XmlWriter - 但我认为 1.1 中不存在这种情况; 所以并不是完全有用 - 但只是多了一个“考虑升级”的事情......是的,我意识到这并不总是可能的。

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.

二手情话 2024-07-12 21:29:25

我做了一个小修正

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

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();
}
纵情客 2024-07-12 21:29:25

以下链接将带您到一篇文章,其中有人提供了一种通过使用 XmlWriter 来抑制处理指令并进入“元素”状态而不是“开始”状态的方法。 这会导致处理指令无法写入。

抑制处理指令< /a>

如果将 XmlWriter 传递给序列化器,它只会发出一个处理
如果 XmlWriter 的状态为“开始”(即没有任何内容)
尚未写入)。

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

在这种情况下,编写器将处于“元素”状态(在元素内部)
所以不会写任何处理指令。 一个你写完的
XML,您可以从底层流中提取文本并将其处理为
你心满意足。

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.

我乃一代侩神 2024-07-12 21:29:25

省略命名空间怎么样?

而不是使用

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\">

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\">
帅冕 2024-07-12 21:29:25

这适用于 .NET 1.1。 (但你还是应该考虑升级)

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

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() { }
   }
夏花。依旧 2024-07-12 21:29:25

如果“处理指令”指的是 xml 声明,那么您可以通过设置 XmlWriterSettings 的 OmitXmlDeclaration 属性来避免这种情况。 您需要使用 XmlWriter 进行序列化才能完成此任务。

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

但是啊,这并不能回答你关于 1.1 的问题。 嗯,供其他人参考。

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.

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