如何将对象序列化为原始 XML

编程入门 行业动态 更新时间:2024-10-20 05:39:54
本文介绍了如何将对象序列化为原始 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

背景:我正在研究这个集成解决方案,我必须在其中使用自定义故障合同实现 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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:24:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   如何将   原始   对象   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!