XmlSerializer的序列化到使用这两种标签的空变量? [英] XmlSerializer Serialize empty variable to use both tags?

查看:296
本文介绍了XmlSerializer的序列化到使用这两种标签的空变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够到一个序列化的XML类装载到一个SOAP信封。我开始,所以我不充盈的内脏所以会出现这样的:

I want to be able to load a serialized xml class to a Soap Envelope. I am starting so I am not filling the innards so it appears like:

<Envelope    
xmlns="http://schemas.xmlsoap.org/soap/envelope/" />



我希望它看起来像:

I want it to appear like:

<Envelope    
xmlns="http://schemas.xmlsoap.org/soap/envelope/" ></Envelope>`


我写的类是这样的:

The class I wrote is this:

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/",ElementName="Envelope", IsNullable = true)]
public class TestXmlEnvelope
{
  [System.Xml.Serialization.XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  public System.Collections.ArrayList Body = new System.Collections.ArrayList();
} //class TestXmlEnvelope`



我使用这是因为其他人的例子可能想在一个单独的元素。我相信这一定是简单的,但可悲的是,我不知道这样做的正确的关键字。

I am using this as an example since other people might want it in an individual element. I am sure this must be simple but sadly I don't know the right keyword for this.

一如往常感谢您的帮助。

As always thanks for your help.

当我尝试使用这个指令

The error comes when I try to use this instruction

System.Xml.Serialization.XmlSerializer xmlout = new System.Xml.Serialization.XmlSerializer(typeof(TestXmlEnvelope));
System.IO.MemoryStream memOut = new System.IO.MemoryStream();
xmlout.Serialize(memOut, envelope, namespc);
Microsoft.Web.Services.SoapEnvelope soapEnv = new Microsoft.Web.Services.SoapEnvelope();
soapEnv.Load(memOut);



它给了我错误根元素未找到。

It gives me error "Root Element not found".

我固定错误的问题是,当我序列化的对象,我没有设置memOut.Position = 0。不过我希望这个问题能帮助其他可能希望人们能做到这一点。

I fixed the error the problem was that after I serialized the object I didn't set the memOut.Position = 0. Still I hope this question helps other people that may want to do this.

推荐答案

这里的主要问题是,的XmlSerializer 通话对writeEndElement()的XmlWriter 时,它会写一个结束标记。然而,这产生的简写<标签/> 的形式时,有没有内容。在 WriteFullEndElement()单独写结束标签。

The main issue here is that the XmlSerializer calls WriteEndElement() on the XmlWriter when it would write an end tag. This, however, generates the shorthand <tag/> form when there is no content. The WriteFullEndElement() writes the end tag separately.

您可以注入自己的的XmlTextWriter 进入中间的那个串行随后将用它来展示该功能。

You can inject your own XmlTextWriter into the middle that the serializer would then use to exhibit that functionality.

由于串行是适当的的XmlSerializer ,试试这个:

Given that serializer is the appropriate XmlSerializer, try this:

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink) : base(sink) { }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

...

var writer = new XmlTextWriterFull(innerwriter);
serializer.Serialize(writer, obj);

为您添加的代码的情况下,增加的外观构造函数:

for the case of your added code, add facade constructors for:

public XmlTextWriterFull(Stream stream, Encoding enc) : base(stream, enc) { }
public XmlTextWriterFull(String str, Encoding enc) : base(str, enc) { }

然后,使用内存流作为你的内心流构造函数前:

Then, use the memory stream as your inner stream in the constructor as before:

System.IO.MemoryStream memOut = new System.IO.MemoryStream();
XmlTextWriterFull writer = new XmlTextWriterFull(memOut, Encoding.UTF8Encoding); //Or the encoding of your choice
xmlout.Serialize(writer, envelope, namespc);

这篇关于XmlSerializer的序列化到使用这两种标签的空变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆