如何将对象序列化为原始 XML [英] How to serialize an object to a raw XML

查看:28
本文介绍了如何将对象序列化为原始 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在研究这个集成解决方案,我必须在其中使用自定义故障合同实现 WCF 服务 (BizTalk).故障信息应如下所示,

Background : I am working on this integration solution where I have to implement a WCF service (BizTalk) with a custom fault contract. The fault message should look like follows,

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode>002</faultcode>
         <faultstring>some fault</faultstring>
         <detail>
               <wor:invalidMessageFault xmlns:wor="somenamespace">
                  <faultCode>002</faultCode>
                  <faultReference>Client WebService</faultReference>
                  <faultText>some fault</faultText>
               </wor:invalidMessageFault>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

到目前为止:我已经创建了一个自定义故障检查器来拦截故障消息并将故障发回.

So far: I have created a custom fault inspector to intercept the fault message and send back the fault.

问题:我需要构建故障消息的<detail>部分,据我所知,唯一的方法是转储原始xml进入它,因为在故障消息构造中,

Problem : I need to construct the <detail> section of the fault message and as far as I figured out only way to do it is to dump raw xml into it, because in the fault message construction,

var faultException = new FaultException<RawXMLString>(raw, fault.faultText, new FaultCode(fault.faultCode)).CreateMessageFault();

它只接受一个对象(可以序列化)作为细节,我尝试了不同的东西,但我可以用对象构造相同的消息.

It only accept an object (which can be serialized) as detail, and I tried different things but I could construct the same message with object.

最后我想到了使用自定义序列化来生成准确的消息,

Finally I thought of using a custom serialization to generate the exact message,

public class RawXMLString : IXmlSerializable
{
    private string xmlTemplate = @"<wor:invalidMessageFault xmlns:wor="some namespace">
          <faultCode>{0}</faultCode>
          <faultReference>Client WebService</faultReference>
          <faultText>{1}</faultText>
        </wor:invalidMessageFault>";

    public string FaultCode { get; set; }

    public string FaultText { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteRaw(string.Format(xmlTemplate,FaultCode,FaultText));
    }
}

现在还有一个问题,因为我不想要<RawXMLString>标签,有没有办法强制序列化程序忽略根?

Now there is another issue, because I don't want <RawXMLString> tag, is there any way to force serializer to ignore the root?

推荐答案

这符合要求吗?

[XmlRoot(Namespace = "somenamespace",
 ElementName = "invalidMessageFault")]
public class InvalidMessageFault : IXmlSerializable
{
    public string FaultCode { get; set; }

    public string FaultText { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteElementString("faultCode", FaultCode);
        writer.WriteElementString("faultReference", "Client WebService");
        writer.WriteElementString("faultText", FaultText);
    }
}

这篇关于如何将对象序列化为原始 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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